获取网络接口信息

发布于 2024-08-05 11:28:59 字数 239 浏览 7 评论 0原文

我知道有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔少女心 2024-08-12 11:28:59

Python 很好 :D 但让我们看看 bash:

Interfaces=`ifconfig -a \
    | grep -o -e "[a-z][a-z]*[0-9]*[ ]*Link" \
    | perl -pe "s|^([a-z]*[0-9]*)[ ]*Link|\1|"`

for Interface in $Interfaces; do
    INET=`ifconfig $Interface | grep -o -e "inet addr:[^ ]*" | grep -o -e "[^:]*$"`
    MASK=`ifconfig $Interface | grep -o -e "Mask:[^ ]*"      | grep -o -e "[^:]*$"`
    STATUS="up"
    if [ "$INET" == "" ]; then
        INET="-"
        MASK="-"
        STATUS="down";
    fi
    printf "%-10s %-15s %-16s %-4s\n" "$Interface" "$INET" "$MASK" "$STATUS"
done

它非常简单。

执行此操作的假设是“ifconfig 接口 不显示 Internet 地址”,这意味着该接口已关闭。

我希望这有帮助。

Python is good :D but let see in bash:

Interfaces=`ifconfig -a \
    | grep -o -e "[a-z][a-z]*[0-9]*[ ]*Link" \
    | perl -pe "s|^([a-z]*[0-9]*)[ ]*Link|\1|"`

for Interface in $Interfaces; do
    INET=`ifconfig $Interface | grep -o -e "inet addr:[^ ]*" | grep -o -e "[^:]*$"`
    MASK=`ifconfig $Interface | grep -o -e "Mask:[^ ]*"      | grep -o -e "[^:]*$"`
    STATUS="up"
    if [ "$INET" == "" ]; then
        INET="-"
        MASK="-"
        STATUS="down";
    fi
    printf "%-10s %-15s %-16s %-4s\n" "$Interface" "$INET" "$MASK" "$STATUS"
done

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.

送君千里 2024-08-12 11:28:59

ifconfig 有两种输出模式——默认模式提供更多输出,而短 -s 模式提供较少输出(或者更确切地说,选择与您想要的不同的信息)。那么,在默认模式下使用 ifconfig 并在脚本中挑选您想要的特定信息(python、perl、ruby、awk、bash+sed+...,无论您喜欢什么;-)怎么样?例如,w/Python:

import re
import subprocess

ifc = subprocess.Popen('ifconfig', stdout=subprocess.PIPE)
res = []
for x in ifc.stdout:
  if not x.strip():
    print ' '.join(res)
    del res[:]
  elif not res:
    res.append(re.match(r'\w+', x).group())
  else:
    mo = re.match(r'\s+inet addr:(\S+).*Mask:(\S+)', x)
    if mo:
      res.extend(mo.groups())
    elif re.match(r'\sUP\s', x):
      res.append('up')
    elif re.match(r'\sDOWN\s', x):
      res.append('down')

if res: print ' '.join(res)

输出应该如您所愿(我希望很容易翻译成我提到的任何其他语言)。

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:

import re
import subprocess

ifc = subprocess.Popen('ifconfig', stdout=subprocess.PIPE)
res = []
for x in ifc.stdout:
  if not x.strip():
    print ' '.join(res)
    del res[:]
  elif not res:
    res.append(re.match(r'\w+', x).group())
  else:
    mo = re.match(r'\s+inet addr:(\S+).*Mask:(\S+)', x)
    if mo:
      res.extend(mo.groups())
    elif re.match(r'\sUP\s', x):
      res.append('up')
    elif re.match(r'\sDOWN\s', x):
      res.append('down')

if res: print ' '.join(res)

and the output should be as you desire it (easy to translate in any of the other languages I mentioned, I hope).

雨落星ぅ辰 2024-08-12 11:28:59

您可能对 ip 命令感兴趣。以下示例重点关注以 CIDR 表示法输出的全局有效 IPv4 地址。

# list interfaces that are up
ip -family inet -oneline addr show scope global | awk '{ printf "%s %s up\n", $2, $4 }'

# list interfaces that are down
ip -family inet -oneline link show scope global | grep ' DOWN ' | sed 's/\://g' | awk '{ printf "%s none down\n", $2}'

(请注意,示例中省略了所需的网络掩码表示形式。)

由于 ip 非常强大,您也许可以使用其他参数找到更清晰的解决方案。

You might be interested in the ip command. The following example focuses on globally valid IPv4 addresses outputting them in CIDR notation.

# list interfaces that are up
ip -family inet -oneline addr show scope global | awk '{ printf "%s %s up\n", $2, $4 }'

# list interfaces that are down
ip -family inet -oneline link show scope global | grep ' DOWN ' | sed 's/\://g' | awk '{ printf "%s none down\n", $2}'

(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文