gpt4 book ai didi

linux - 提取节点 IP 而不解析 ifconfig 的输出

转载 作者:太空宇宙 更新时间:2023-11-04 12:18:39 24 4
gpt4 key购买 nike

ifconfig 在我的机器上的输出如下:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
ether 3c:a8:2a:22:64:dc txqueuelen 1000 (Ethernet)
RX packets 7574600 bytes 945219457 (901.4 MiB)
RX errors 0 dropped 10 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 16

eth1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether 3c:a8:2a:22:64:dd 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
device interrupt 17

eth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.25.26.78 netmask 255.255.254.0 broadcast 10.25.27.255
inet6 fe80::8edc:d4ff:feab:48a0 prefixlen 64 scopeid 0x20<link>
ether 8c:dc:d4:ab:48:a0 txqueuelen 1000 (Ethernet)
RX packets 1459110828 bytes 2115198897739 (1.9 TiB)
RX errors 0 dropped 32460 overruns 0 frame 0
TX packets 1527389441 bytes 2229468090451 (2.0 TiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth3: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether 3c:a8:2a:22:64:de 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
device interrupt 16

eth4: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether 3c:a8:2a:22:64:df 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
device interrupt 17

eth5: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether 8c:dc:d4:ab:48:a1 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

eth6: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether 8c:dc:d4:a8:a0:e8 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

eth7: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether 8c:dc:d4:a8:a0:e9 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 77128853 bytes 11309319030 (10.5 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 77128853 bytes 11309319030 (10.5 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

因为我想从eth2中提取IP(不同的节点也可以在eth2、eth5等中有IP)。总而言之,我将 IP 提取为:

/sbin/ifconfig|grep 'inet addr'|grep -v '127.0.0.1'|head -1|awk '{print $2}'|awk -F':' '{print $2}'

不过,我得到的输出是空字符串。我哪里出错了?

我想提取 ip,我们提供了 inet 和网络掩码并且 inet 不应该是 127.0.0.1。

最佳答案

首先,如果您不想解析 ifconfig 的输出,您可以使用

hostname -i

它将提供分配给您的主机名的 IP 地址。

如果你想解析上面的输出并只查找 IPv4 地址,你可以试试这个:

cat test.txt |grep 'inet'|grep -v '127.0.0.1' | awk '{ print $2 }' | grep -oE "\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"

关于linux - 提取节点 IP 而不解析 ifconfig 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46660736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com