linux系统下获取所有处于Down状态的接口的shell
ip link | grep "state DOWN"
3: em2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
4: em3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
5: em4: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
136: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500
142: ovs-system: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN
143: ovsbr0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN
linux系统下获取所有处于UP状态的接口的shell
ip link | grep "state UP"
2: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
使用awk匹配到第二个字段,然后sed替换掉冒号。
ip link | grep "state DOWN" | awk '{ print $2 }' | sed 's/://g'
em2
em3
em4
virbr0-nic
ovs-system
ovsbr0
另一种有缺陷的方式:
ip route show
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
10.12.28.0/22 dev br0 proto kernel scope link src 10.12.28.59
169.254.0.0/16 dev br0 scope link metric 1006
default via 10.12.28.1 dev br0
ip route show | awk '{ print $3}'
virbr0
br0
br0
10.12.28.1
注意最后一显示的默认路由:default via 10.12.28.1 dev br0
这种方式需要匹配到 dev这个字段后的第一个字段,而且没有显示接口状态是否是up的功能。