使用 ifconfig 和 awk 仅显示网络接口和 IP 地址

display only network interface and ip address using ifconfig and awk

我对 ifconfig 输出的所有信息不感兴趣,我只想知道网络接口及其各自的 ip 地址。

我可以使用以下命令从 ifconfig 获取网络接口:

ifconfig |grep "      L" |  awk '{ print }'
eth0
lo
tun0

和每个接口的IP地址用命令

ifconfig |grep "inet:" | cut -d: -f2 | awk '{ print }'
192.168.0.10
127.0.0.1
10.5.0.13

如何只用一个命令或脚本同时显示数据、网络接口和 ip 地址?

eth0 - 192.168.0.10
lo - 127.0.0.1
tun0 - 10.5.0.13

您可以使用 ip route show 来获取您想要的信息。例如使用此命令过滤:

ip r show|grep " src "|cut -d " " -f 3,12

输出类似:

eth0 192.168.1.114