查找所有以太网接口并关联IP地址

查找所有以太网接口并关联IP地址

如何获取所有接口并关联 IP 地址,如下所示

[root@centso ]# ifconfig
enp3s0f0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet addr:10.5.2.10  Bcast:10.5.7.255  Mask:255.255.248.0
        inet6 fe80::e611:5bff:feea:5e50  prefixlen 64  scopeid 0x20<link>
        ether e4:11:5b:ea:5e:50  txqueuelen 1000  (Ethernet)
        RX packets 638000416  bytes 763371981799 (710.9 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16607280  bytes 9787019600 (9.1 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp3s0f1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether e4:11:5b:ea:5e:52  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

enp4s0f0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether e4:11:5b:ea:5e:44  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

enp4s0f1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether e4:11:5b:ea:5e:46  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

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 45015  bytes 4371658 (4.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 45015  bytes 4371658 (4.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

我想要如下输出,我也相信有一个命令ip link或其他东西可以做到这一点,但不记得了,而且有些机器的网卡名称不同,例如enoXethX

enp3s0f0: 10.5.2.10
enp3s0f1:
enp4s0f0:
enp4s0f1:

答案1

我想我发现了一些东西,我确信一定有更聪明的方法,但现在我已经准备好了。

[root@server1 ~]# ip -o -4 addr show | awk '{print $1" " $2": "$4}'
1: lo: 127.0.0.1/8
2: eno1: 192.168.100.190/24
3: eno2: 10.5.8.33/21

答案2

尝试这个:

ip addr show | awk -F '[: ]+' '
    /^[^ ]/      { iface=$2 }               # Start of section, we memorize the interface name
    $2 == "inet" { print iface ": " $3 }    # IPv4 address => print the line
'

答案3

有一个例子:

ip address show | awk '/inet / {split($2,var,"/*"); print $7,":",var[1]}'

输出应该是这样的:

 : 127.0.0.1
enp3s0f0: 10.5.2.10
enp3s0f1: .......
enp4s0f0: .......
enp4s0f1: .......

答案4

此命令显示所有接口及其各自的 IP 地址,在一行中以空格分隔

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

参考:https://stackoverflow.com/questions/38041163/display-only-network-interface-and-ip-address-using-ifconfig-and-awk

相关内容