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

时间:2015-12-02 21:15:09

标签: awk sed grep ifconfig

您好我正在寻找一个命令行来显示接口和与该接口相关的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 
你能帮帮忙吗?

1 个答案:

答案 0 :(得分:3)

尝试以下sed管道:

ifconfig -a | sed -n -e 's/^\([[:alnum:]]\+[[:space:]]\+\).*$/\1/p' -e 's/^[[:space:]]\+\(inet .*\)$/\1/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