ifconfig -a 获取需要的数据 grep

ifconfig -a 获取需要的数据 grep

如果我发出以下命令来获取网络接口数据:

[root@pi lib]# ifconfig -a

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.154  netmask 255.255.255.0  broadcast 192.168.0.255
        ether b8:27:eb:3c:03:fe  txqueuelen 1000  (Ethernet)
        RX packets 44219  bytes 17569207 (16.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 9964  bytes 7176485 (6.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 0  (Local Loopback)
        RX packets 229  bytes 21989 (21.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 229  bytes 21989 (21.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 00:11:6b:f0:bb:31  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@pi lib]#

如何从终端获取网络接口名称、IP、子网掩码、网关和 MAC 地址?

答案1

您最好使用ip a,但根据当前的输出,您可以使用awk

awk '
  BEGIN { RS="\n\n"} 
  /eth0/ && /UP/ {ifc=$1; ip=$6; subn=$8; gway=$10; mac=$12} 
  END {print "Interface: "ifc "\nIP: "ip "\nSubnet: "subn "\nGateway: "gway "\nMac: "mac}
' <(ifconfig -a)

Interface: eth0:
IP: 192.168.0.154
Subnet: 255.255.255.0
Gateway: 192.168.0.255
Mac: b8:27:eb:3c:03:fe

这只会打印接口启动时的详细信息。

答案2

这会帮助你

ip rl && ip addr show {接口名称} | grep 以太

前任。 ip rl && ip addr 显示 eth0 | grep 以太

样本输出

ip r l && ip addr show wlan0 | grep ether default via 192.168.1.254 dev wlan0 proto static 192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.42 link/ether e4:d5:3d:ef:90:a9 brd ff:ff:ff:ff:ff:ff

ip r l这将为您提供网关、IP、接口、网络掩码

ip addr show {interface name}这将为您提供 mac 地址的其余详细信息

相关内容