如何在脚本中获得所有的网络接口,只有它们的ip地址和掩码?Linux



ifconfig显示的信息太多。我需要知道所有的网络接口,无论它们是否可用,并将它们与ip地址和掩码配对。

如果ficonfig返回

enp9s0    Link encap:Ethernet  HWaddr 54:be:f7:5c:99:0d  
UP BROADCAST 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)
Interrupt:18 
lo        Link encap:Локальная петля (Loopback)  
inet addr:127.0.0.1  Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING  MTU:65536  Metric:1
RX packets:256 errors:0 dropped:0 overruns:0 frame:0
TX packets:256 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000 
RX bytes:19992 (19.9 KB)  TX bytes:19992 (19.9 KB)
wlp8s0    Link encap:Ethernet  HWaddr f8:2f:a8:f3:1b:31  
inet addr:192.168.0.102  Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::820d:4dd0:b3f9:10ed/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:241 errors:0 dropped:0 overruns:0 frame:4220
TX packets:301 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000 
RX bytes:101434 (101.4 KB)  TX bytes:101132 (101.1 KB)
Interrupt:17

示例输出:

enp9s0: addr:- Mask:-
lo: addr:127.0.0.1 Mask:255.0.0.0
wlp8s0: addr:192.168.0.102 Mask:255.255.255.0
ifconfig | awk '/^[^ ]/{i=$1; getline; if($1=="inet") {print i": "$2,$NF} else {print i": addr:- Mask:-"}}'

输出:

enp9s0:addr:-掩码:-lo:addr:127.0.0.1掩码:255.0.0.0wlp8s0:addr:192.168.0.102掩码:255.255.255.0

Btw:在Ubuntu中,ifconfig已被弃用,取而代之的是iproute2。

only ip:
ip a|grep -v inet6|grep inet|awk '{print $2}'|sed -e 's/// /g' |awk '{print $1}'
ip with mask:
ip a|grep -v inet6|grep inet|awk '{print $2}'

相关内容

  • 没有找到相关文章

最新更新