如何使用接口名称列出配置有 IP 的网络接口

How to list network interfaces that are configured with IP with the interface name

我正在寻找一个命令行来显示接口和与接口关联的 ip。我 运行 命令 ifconfig -a | grep -inet .. 但我还需要打印界面。我怎样才能打印接口名称?

命令

ifconfig -a | grep inet

输入

eth1      Link encap:Ethernet  HWaddr 40:A8:F0:2D:B3:98
          inet addr:10.33.211.67  Bcast:10.33.211.79      
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth2      Link encap:Ethernet  HWaddr 8C:DC:D4:AD:A6:EF
          inet addr:64.15.238.227  Bcast:64.15.238.239  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3532763832 errors:0 dropped:0 overruns:0 frame:0

eth8      Link encap:Ethernet  HWaddr 40:A8:F0:2D:B3:9A
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:3532763832 errors:0 dropped:0 overruns:0 frame:0

期望的输出

eth1    inet addr:10.33.211.67  Bcast:10.33.211.79 Mask:255.255.255.240
eth2    inet addr:64.15.238.227 Bcast:64.15.238.239  Mask:255.255.255.240
eth8    ----------  blank because it has not been configured 

尝试以下 sed 管道:

ifconfig -a | sed -n -e 's/^\([[:alnum:]]\+[[:space:]]\+\).*$//p' -e 's/^[[:space:]]\+\(inet .*\)$//p' | sed 'N;s/\n/ /'

第一个 sed 命令选择以接口名称或一串空格开头的行,后跟 inet。第二个根据 Putting Two Consecutive Lines into One Line with Perl / AWK 从结果中删除每隔一个换行符。输出将是:

eth1       inet addr:10.33.211.67  Bcast:10.33.211.79      
eth2       inet addr:64.15.238.227  Bcast:64.15.238.239  
eth8