和ip路由2用户空间工具可以使用以下命令显示网络设备ip命令动词link show
(有时缩写为l sh
)。
生成的输出不显示链路/接口设备的类型。
root@box:/# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether bc:97:e1:58:10:18 brd ff:ff:ff:ff:ff:ff
3: eno1np0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether bc:97:e1:58:10:1a brd ff:ff:ff:ff:ff:ff
4: eno2np1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether bc:97:e1:58:10:1b brd ff:ff:ff:ff:ff:ff
每个链接都以冗余的方式告诉两次类似mtu
、UP
/ 的信息DOWN
,但是对我来说没有明显的指示来告诉它的类型。我也找不到任何指示联机帮助页很高兴显示TYPE
,尽管有很多:
TYPE := [ bridge | bridge_slave | bond | bond_slave | can | dummy | hsr | ifb | ipoib | macvlan | macvtap | vcan | veth | vlan | vxlan | ip6tnl | ipip | sit | gre | gretap | erspan | ip6gre | ip6gretap | ip6erspan | vti | vrf | nlmon | ipvlan | lowpan | geneve | macsec ]
ip2route 工具是否有内置方法可以输出列表中的类型?
答案1
接口类型信息很少使用,通常是显示的只需添加-details
选项ip
:
-d
,-details
输出更详细的信息。
因此ip -details link show
会显示所有这些接口的此信息,还有许多其他附加信息,例如:
$ ip -d link show lxcbr0
7: lxcbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
link/ether 00:16:3e:00:00:00 brd ff:ff:ff:ff:ff:ff promiscuity 0 minmtu 68 maxmtu 65535
bridge forward_delay 1500 hello_time 200 max_age 2000 ageing_time 30000 stp_state 0 priority 32768 vlan_filtering 0 vlan_protocol 802.1Q bridge_id 8000.0:16:3e:0:0:0 designated_root 8000.0:16:3e:0:0:0 root_port 0 root_path_cost 0 topology_change 0 topology_change_detected 0 hello_timer 0.00 tcn_timer 0.00 topology_change_timer 0.00 gc_timer 34.76 vlan_default_pvid 1 vlan_stats_enabled 0 vlan_stats_per_port 0 group_fwd_mask 0 group_address 01:80:c2:00:00:00 mcast_snooping 1 mcast_router 1 mcast_query_use_ifaddr 0 mcast_querier 0 mcast_hash_elasticity 16 mcast_hash_max 4096 mcast_last_member_count 2 mcast_startup_query_count 2 mcast_last_member_interval 100 mcast_membership_interval 26000 mcast_querier_interval 25500 mcast_query_interval 12500 mcast_query_response_interval 1000 mcast_startup_query_interval 3124 mcast_stats_enabled 0 mcast_igmp_version 2 mcast_mld_version 1 nf_call_iptables 0 nf_call_ip6tables 0 nf_call_arptables 0 addrgenmode eui64 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
位于bridge
第三行的开头。
使用 JSON 输出jq
命令(这是从 shell 处理 JSON 时的必备工具)允许可靠地解析命令的输出,如果只想沿着接口名称检索此信息,则无需事先知道类型。
$ ip -details -json link show | jq --join-output '.[] | .ifname," ",.linkinfo.info_kind,"\n"'
lo null
dummy0 dummy
dummy2 dummy
lxcbr0 bridge
wlan0 null
eth0 null
virbr0 bridge
virbr0-nic tun
tap0 tun
veth0 veth
test veth
wireguard0 wireguard
vethZ0ZQFJ veth
真正的接口(以及lo
)没有类型(即.[].linkinfo.info_kind
不存在)并且杰克将返回无效的对于一个不存在的字段。可以用这个来过滤掉它:
ip -details -json link show | jq --join-output '.[] | .ifname," ", if .linkinfo.info_kind != null then .linkinfo.info_kind else empty end, "\n"'
实际上,搜索功能ip link show
将种类和奴隶类作为类型,详细输出将在第 3 行显示一个,另一个在第 4 行显示。在 JSON 输出中,这是两个不同的字段:.[].linkinfo.info_kind
和.[].linkinfo.info_slave_kind
,因此从属类型需要其他命令,与显示这两个字段相同。这是两者的示例:
ip -details -json link show | jq --join-output '
.[] |
if .ifname != null then
.ifname,
" ",
if .linkinfo.info_kind != null then
.linkinfo.info_kind
else
empty
end,
" ",
if .linkinfo.info_slave_kind != null then
.linkinfo.info_slave_kind
else
empty
end,
"\n"
else
empty
end
'
它输出:
lo
dummy0 dummy
dummy2 dummy
lxcbr0 bridge
wlan0
eth0
virbr0 bridge
virbr0-nic tun bridge
tap0 tun
veth0 veth
test veth
wireguard0 wireguard
vethZ0ZQFJ veth bridge
并显示在这里virbr0-nic成为一个屯(真的通塔普事实上它是屯或者轻敲是在一个子领域)设备以及桥奴隶,以及vethZ0ZQFJA韦斯设备以及桥奴隶。
这都儿一样杰克当查询从属接口时,上面的过滤器也将处理过滤后的输出ip ... link show ... type ...slave
,这显然会通过忽略没有接口名称的(空)条目来为不匹配的接口返回额外的空对象。因此,以 开头的行ip -details -json link show type bridge_slave |
只会返回:
virbr0-nic tun bridge
vethZ0ZQFJ veth bridge
答案2
这是我用来获取信息的脚本
#!/bin/bash
TYPES="bridge bridge_slave bond bond_slave can dummy hsr ifb ipoib macvlan macvtap vcan veth vlan vxlan ip6tnl ipip sit gre gretap erspan ip6gre ip6gretap ip6erspan vti vrf nlmon ipvlan lowpan geneve macsec"
for TYPE in $TYPES
do
echo "TYPE $TYPE"
ip link show type "$TYPE"
done
这会将信息插入到输出中,例如ip link show
#!/bin/bash
TYPES="bridge bridge_slave bond bond_slave can dummy hsr ifb ipoib macvlan macvtap vcan veth vlan vxlan ip6tnl ipip sit gre gretap erspan ip6gre ip6gretap ip6erspan vti vrf nlmon ipvlan lowpan geneve macsec"
# intially query all links
# (since output is 2 lines per link, squash into single line, and sort,
# so we can use `comm`)
LINK_ALL="$(ip link show | paste - - | sort)"
# initial empty
LINK_RESULT=""
# populate $LINK_RESULT
for TYPE in $TYPES
do
# using specific `ip link show type`
# ( again use `paste` and sort
# -> each link info being 2 lines is put in one
# -> sorting required for `comm` )
LINK_TYPE="$(ip link show type "$TYPE" | paste - - | sort)"
# where there any links of this type ?
test -n "$LINK_TYPE" && {
# then remove from the initial set
LINK_ALL="$(comm -3 <(echo "$LINK_ALL") <(echo "$LINK_TYPE"))"
# add to result set (and insert link TYPE information)
LINK_RESULT="$(echo "$LINK_RESULT"; echo "$LINK_TYPE" | sed 's/>/>'" type=$TYPE"'/g')"
}
done
# Since ordnary NICs and lo have no type they have not been found before
# join them
LINK_RESULT="$LINK_ALL $(echo "$LINK_RESULT")"
# Output result undoing the paste
echo "$LINK_RESULT" | sed 's:link/:\n link/:g'