gpt4 book ai didi

linux - 仅在Linux中打印IP和设备名称

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

我想显示设备名称和 IP。谁能知道我如何提取它?

ifconfig -a 

eth0 Link encap:Ethernet HWaddr 10:60:4b:db:60:86
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

lo Link encap:Local Loopback
inet addr: 1.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:1366 errors:0 dropped:0 overruns:0 frame:0
TX packets:1366 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:245049 (245.0 KB) TX bytes:245049 (245.0 KB)

最佳答案

我可以提议:

ifconfig -a | tr '\n' '~' | sed 's/~~/\n/g' | \
sed 's/^\([^ ]*\) .*inet addr:\([0-9.]*\) .*$\|^\([^ ]*\) .*$/\3\1 \2/g'

输出:

eth0 10.0.41.21
eth1
lo 127.0.0.1

或者,如果您想省略任何没有 IP 地址的接口(interface):

ifconfig -a | tr '\n' '~' | sed 's/~~/\n/g' | grep 'inet addr:' | \
sed 's/^\([^ ]*\) .*inet addr:\([0-9.]*\) .*$/\1 \2/g'

输出:

eth0 10.0.41.21
lo 127.0.0.1

其工作原理是将所有换行符转换为波形符,然后将接口(interface)之间的双换行符转换回单换行符,以便每个接口(interface)都位于一行上。然后,对于每一行,它只提取所需的信息(接口(interface)名称和 IP 地址)并用它们替换整行。

(自被接受以适应没有 IP 地址的接口(interface)以来已进行编辑,根据下面的评论。)

关于linux - 仅在Linux中打印IP和设备名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23578739/

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