获取网络接口信息
我知道有 ifconfig 命令,我们可以列出网络接口信息。 但我想获取以下模式的信息,
Interface_Name IP_Address Net_Mask Status(up/down)
例如
eth0 192.168.1.1 255.255.255.0 down
我尝试了 ifconfig 和 grep 命令,但无法获得正确的模式。 还有另一个命令或一些技巧可以做到这一点吗?
I know there is ifconfig command that we can list network interface info.
but i want to get information in the following pattern
Interface_Name IP_Address Net_Mask Status(up/down)
for example
eth0 192.168.1.1 255.255.255.0 down
I tried ifconfig and grep command but can't get right pattern.
There is another command or some trick to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Python 很好 :D 但让我们看看 bash:
它非常简单。
执行此操作的假设是“
ifconfig 接口
不显示 Internet 地址”,这意味着该接口已关闭。我希望这有帮助。
Python is good :D but let see in bash:
It is quite straightforward.
This is done on an assumption that '
ifconfig interface
does not show an internet address' to means that the interface is down.I hope this helps.
ifconfig
有两种输出模式——默认模式提供更多输出,而短-s
模式提供较少输出(或者更确切地说,选择与您想要的不同的信息)。那么,在默认模式下使用 ifconfig 并在脚本中挑选您想要的特定信息(python、perl、ruby、awk、bash+sed+...,无论您喜欢什么;-)怎么样?例如,w/Python:输出应该如您所愿(我希望很容易翻译成我提到的任何其他语言)。
ifconfig
has two output modes -- the default one in which it gives a LOT more output, and the short-s
one in which it gives less (or, rather, picks different bits of info from what you'd like). So what about taking ifconfig in the default mode and cherry-picking the specific info you want in a script (python, perl, ruby, awk, bash+sed+..., whatever floats your boat;-). E.g., w/Python:and the output should be as you desire it (easy to translate in any of the other languages I mentioned, I hope).
您可能对
ip
命令感兴趣。以下示例重点关注以 CIDR 表示法输出的全局有效 IPv4 地址。(请注意,示例中省略了所需的网络掩码表示形式。)
由于
ip
非常强大,您也许可以使用其他参数找到更清晰的解决方案。You might be interested in the
ip
command. The following example focuses on globally valid IPv4 addresses outputting them in CIDR notation.(Note that the desired netmask representation is omitted in the example.)
As
ip
is quite powerful, you might be able to find an even cleaner solution using other parameters.