如何仅使用其 IP 地址和掩码在脚本中获取所有网络接口? Linux

How to get all net interfaces in script only with their ip addresses and masks? Linux

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

如果ficonfig returns

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=; getline; if(=="inet") {print i": ",$NF} else {print i": addr:- Mask:-"}}'

输出:

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

顺便说一句:在 Ubuntu 中,ifconfig 已弃用并由 iproute2 取代。

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