2017-06-14 49 views
0

這是標準的Linux ifconfig命令Linux發行多個IP地址(使用ifconfig)在1個輸出

[email protected]:~$ ifconfig 
eth0  Link encap:Ethernet HWaddr 00:00:00:00:00:10 
      inet addr:192.168.1.1 Bcast:192.168.56.255 Mask:255.255.255.0 
      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 
      RX packets:112 errors:0 dropped:0 overruns:0 frame:0 
      TX packets:93 errors:0 dropped:0 overruns:0 carrier:0 
      collisions:0 txqueuelen:1000 
      RX bytes:14616 (14.2 KiB) TX bytes:17776 (17.3 KiB) 

eth1  Link encap:Ethernet HWaddr 00:00:00:00:00:11 
      inet addr:10.0.1.1 Bcast:10.0.1.255 Mask:255.255.255.0 
      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
      collisions:0 txqueuelen:1000 
      RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) 

[email protected]:~$ 

這是從使用ifconfig

[email protected]:~$ cat script.sh 
ifconfig | grep ad.*Bc | cut -d: -f2 | awk '{ print $1}' 
[email protected]:~$ 

輸出看起來像只打印IP地址這

[email protected]:~$ ./script.sh 
192.168.1.1 
10.0.1.1 
[email protected]:~$ 

我試圖做到的是打印的IP地址的輸出在1號線...像這樣的東西。

[email protected]:~$ ./script2.sh 
192.168.1.1 10.0.1.1 
[email protected]:~$ 

ifconfig可以嗎?如果是的話,我將不勝感激,如果你能分享這個竅門。由於

+0

看到這個https://stackoverflow.com/questions/23934425/parse-ifconfig-to-get-only-my-ip-address-using-bash ?rq = 1 – manishr

+0

@manishr,這是不同的。這是僅打印IP地址的輸出(默認是將每個IP地址打印在新行中,而不是1行)。順便說一句,我已經提供了關於該鏈接的答案。您可能需要再次檢查該鏈接。謝謝 我在這裏想要完成的是打印1行輸出的IP地址,zhenguoli已經提供了準確的答案 –

回答

1
ifconfig | grep ad.*Bc | cut -d: -f2 | awk '{ print $1}' | { tr '\n' ' '; echo; } 

你的代碼幾乎是功能性的。只需將換行符轉換爲空格並添加更多換行符即可。

+1

擊敗我吧+1 – Wossname

1

如果你有hostname

hostname -I 

測試:

$ hostname -I 
192.168.2.253 192.168.0.179 
+0

-I(首都)不支持在這個框中 user @ BusyBox:〜 $主機名-I 主機名:無效選項 - '我' BusyBox v1.24.2(2016-05-16 13:46:43 UTC)多重調用二進制。 用法:主機名[選項] [主機名| -F FILE] 獲取或設置主機名或DNS域名 -s短 -i地址的主機名 -d DNS域名 -f完全合格域名 -F文件中使用文件的內容作爲主機名 用戶@BusyBox:〜$ –

0
ifconfig | awk '{match($0,/inet addr:([^ ]+)/,a);x=x FS a[1]}END {print x} ' 
    142.133.152.191   127.0.0.1 

或者使用grep

ifconfig | grep -oP 'inet addr:\K[^ ]+' | xargs 
142.133.152.191 127.0.0.1