使用 ip 命令仅显示具有 ip 地址的网络接口

使用 ip 命令仅显示具有 ip 地址的网络接口

我使用ip获取我机器的 IP 地址。有时,当我有 20 多个网络接口时,识别起来会变得很困难。

有没有办法只显示具有 IP 的网络接口?没有 IP 地址的网络接口应被忽略,不应显示。

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 4c:cc:6a:32:42:0b brd ff:ff:ff:ff:ff:ff
inet 10.76.99.232/23 brd 10.76.99.255 scope global noprefixroute dynamic eno1
   valid_lft 81438sec preferred_lft 81438sec
inet6 fe80::4cd3:b92f:e2:54c8/64 scope link noprefixroute 
   valid_lft forever preferred_lft forever
3: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 52:54:00:67:f0:89 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
   valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:67:f0:89 brd ff:ff:ff:ff:ff:ff   
5: virbr1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:8e:77:61 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.1/24 brd 192.168.100.255 scope global virbr1
   valid_lft forever preferred_lft forever
6: virbr1-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr1 state DOWN group default qlen 1000
link/ether 52:54:00:8e:77:61 brd ff:ff:ff:ff:ff:ff
7: br-251ee4bbddbc: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
link/ether 02:42:59:14:5d:96 brd ff:ff:ff:ff:ff:ff
inet 172.20.0.1/16 brd 172.20.255.255 scope global br-251ee4bbddbc
   valid_lft forever preferred_lft forever
inet6 fe80::42:59ff:fe14:5d96/64 scope link 
   valid_lft forever preferred_lft forever
8: br-7d1010889446: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
link/ether 02:42:e9:35:47:a7 brd ff:ff:ff:ff:ff:ff
inet 172.19.0.1/16 brd 172.19.255.255 scope global br-7d1010889446
   valid_lft forever preferred_lft forever
9: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
link/ether 02:42:a2:bf:15:a8 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
   valid_lft forever preferred_lft forever
inet6 fe80::42:a2ff:febf:15a8/64 scope link 
   valid_lft forever preferred_lft forever
11: veth42032d5@if10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-251ee4bbddbc state UP group default 
link/ether 56:1f:89:31:03:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::541f:89ff:fe31:302/64 scope link 
   valid_lft forever preferred_lft forever 
13: veth6b578c7@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
link/ether da:12:96:51:48:48 brd ff:ff:ff:ff:ff:ff link-netnsid 1
inet6 fe80::d812:96ff:fe51:4848/64 scope link 
   valid_lft forever preferred_lft forever
15: veth36b9b3b@if14: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-251ee4bbddbc state UP group default 
link/ether 9e:89:cb:57:36:67 brd ff:ff:ff:ff:ff:ff link-netnsid 2
inet6 fe80::9c89:cbff:fe57:3667/64 scope link 
   valid_lft forever preferred_lft forever
34: br-22ddff6ae6ef: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
link/ether 02:42:d7:77:d5:2d brd ff:ff:ff:ff:ff:ff
inet 172.18.0.1/16 brd 172.18.255.255 scope global br-22ddff6ae6ef
   valid_lft forever preferred_lft forever

预期产出(大约):

1. eno1: 10.76.99.232 
3. virbr0: 192.168.122.1
5. virbr1: 192.168.100.1 

ETC。

我正在寻找任何使用ip工具进行过滤的本地方法。使用其他工具来获得预期的输出也是值得赞赏的。

答案1

ip -4 -brief address show

将显示最少的地址信息,顺便说一下,只显示具有 IPv4 地址的接口,因为被告知只显示 IPv4(即:不是以太网也不是 IPv6 地址)。

此选项最近(2015 年)添加到 iproute2 实用程序中,并花了一些时间才转移到发行版。例如,它是:

  • 在 Debian 9 中可用,但在 Debian 8 中不可用
  • 在 RHEL 7.5 中可用,但在一些以前的 RHEL 7 版本中不可用。

上游提交:

添加对链接和地址的简要输出支持

输出可能以以下内容开头:

$ ip -4 -brief address show
lo               UNKNOWN        127.0.0.1/8 
eno1             UP             10.76.99.232/23
virbr0           UP             192.168.122.1/24 
virbr1           UP             192.168.100.1/24 

...

答案2

ip -o addr show  

是一个选项。它在一行上显示每个 IP 地址,对于具有多个 IP 地址的接口,显示多行;对于没有地址的接口,则不显示任何行。

man ip

相关内容