[英]using bash how can i get the network device name and ip address in one line?
I want to get the Network Interface device name (ens###) along with its associated IP address (###.###.###.###). 我想获取网络接口设备名称(ens ###)及其关联的IP地址(###。###。###。###)。 I have solutions to get one or the other but I have not been able to find something that can output each pair (Name + IP) to a line.
我有解决方案来获得一个或另一个,但我找不到能够将每对(Name + IP)输出到一行的东西。
Here is a command to get IP 这是获取IP的命令
ip address | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
And here I can get the device name 在这里,我可以得到设备名称
ip address | grep -v lo | cut -d ' ' -f2 | tr ':' '\n' | awk NF
However I would like a way to get both which would output each set to their own line, something like this 但是我想要一种方法来获得两个输出每个集合到他们自己的行,这样的事情
ens32 10.0.0.100
ens33 10.1.0.100
EDIT: 编辑:
Here is a sample output of ip address 这是ip地址的示例输出
[root@centos ~]# ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.100/23 brd 10.0.1.255 scope global dynamic ens32
valid_lft 83040sec preferred_lft 83040sec
inet6 0000::000:0000:0000:0000/64 scope link
valid_lft forever preferred_lft forever
3: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
inet 10.1.0.100/24 brd 10.0.2.255 scope global dynamic ens33
valid_lft 1277sec preferred_lft 1277sec
inet6 0000::000:0000:0000:0000/64 scope link
valid_lft forever preferred_lft forever
SOLUTIONS: 解决方案:
Both of these will give me the same desired output. 这两个都会给我相同的期望输出。 Thanks for the help!
谢谢您的帮助!
ip -o addr show scope global | awk '/^[0-9]:/{print $2, $4}' | cut -f1 -d '/'
ip -o addr show scope global | tr -s ' ' | tr '/' ' ' | cut -f 2,4 -d ' '
Well, awk
, as always, works like a charm. 嗯,像往常一样,
awk
就像一个魅力。
ip address |
awk '
/^[0-9]:/{
name=substr($2, 1, length($2) - 1)
}
/^[ ]*inet /{
split($2, a, "/")
if (name != "lo")
print name,a[1]
}
'
will ouptut: 将ouptut:
ens32 10.0.0.100
ens33 10.1.0.100
:
with substr. :
with substr。 lo
we don't print the output, thus filtering loopback interface. lo
,则不打印输出,从而过滤环回接口。 If you need a one-liner, try this (thanks to Dougie for the more refined ip
command) : 如果你需要一个单行,请尝试这个(感谢Dougie提供更精确的
ip
命令):
ip -oneline -4 addr show scope global | tr -s ' ' | tr '/' ' ' | cut -f 2,4 -d ' '
-oneline
forces output for each interface to a single line. -oneline
将每个接口的输出强制为单行。
Then we cut
out just the interface name and IP from the output, tr
anslating it a bit along the way (for cut to get rid of extra stuff). 然后,我们
cut
出只是接口名称和IP从输出, tr
anslating有点沿途(用于切摆脱额外的东西)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.