我机器中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和netmask,而且inet不应该是127.0.0.1。
答案 0 :(得分:0)
首先,如果您不想解析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"
答案 1 :(得分:0)
我出错了什么?
您正在寻找 inet addr ...
在inet和netmask上查找不是localhost的ip。
$ ifconfig | grep -oP 'inet \K(?!127\.0\.0\.1)[0-9.]+(?=.*netmask [0-9.]+)'
10.25.26.78
使用PRCE正则表达式\K和lookaheads。
答案 2 :(得分:0)
ifconfig | egrep&#39; inet | netmask | broadcast&#39; | grep -v&#39; 127.0.0.1&#39; | awk&#39; {print $ 2}&#39; | awk&#39; {print $ 1}&#39;
答案 3 :(得分:0)
有几种方法可以提取主机的IP地址。
hostname -i | cut -d' ' -f2
gethostip $HOSTNAME | cut -d' ' -f2
如果您仍想查询网络接口,则应使用ip
代替ifconfig
。
ip -o -f inet addr show scope global | awk -F'[ /]+' '{print $4}'
或者
ip -o -f inet addr show scope global | sed -n 's/.*inet \([^\/]*\)\/.*/\1/p'
在您的特定情况下,您只想提取eth2的IP地址。而不是scope global
,您将使用dev eth2
。
ip -o -f inet addr show dev eth2 | awk -F'[ /]+' '{print $4}'