2015-12-02 86 views
0

嗨我正在尋找命令行來顯示與接口相關的接口和ip。我運行命令ifconfig -a | grep -inet ..但我需要打印的界面也。我怎樣才能打印接口名稱?如何列出使用接口名稱配置IP的網絡接口

命令

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 

能否請你幫忙嗎?

+0

Mask應該從哪裏來? – Marged

+0

我無權訪問dev/net,但未以root身份登錄 - 掩碼也來自上面的命令ifconfig -a | grep inet - – theuniverseisflat

回答

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  
+0

這真的是(l)ick ;-) – Marged

+0

很好,謝謝你 - 很棒! – theuniverseisflat

+0

如果我對每個「很好」的評論都讚不絕口:-) –