我想通过mac地址获取设备名称,使用shell。例如,mac =“ec:f4:bb:e1:0c:1d”,原始字符串是这样的:
[root@yf-mos-test-net09 ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
link/ether ec:f4:bb:e1:0c:1c brd ff:ff:ff:ff:ff:ff
3: eth3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
link/ether ec:f4:bb:e1:0c:1d brd ff:ff:ff:ff:ff:ff
6: bond0: <BROADCAST,MULTICAST,MASTER> mtu 1500 qdisc noqueue state DOWN mode DEFAULT
link/ether 1a:e5:33:75:83:f2 brd ff:ff:ff:ff:ff:ff
7: bond1: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT
link/ether 8e:cd:30:03:ea:54 brd ff:ff:ff:ff:ff:ff
923: enp1s16: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond1 state UP mode DEFAULT qlen 1000
link/ether 8e:cd:30:03:ea:54 brd ff:ff:ff:ff:ff:ff
924: enp1s16f1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond1 state UP mode DEFAULT qlen 1000
link/ether 8e:cd:30:03:ea:54 brd ff:ff:ff:ff:ff:ff
973: ovs-netdev: <BROADCAST,PROMISC> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 500
link/ether aa:21:35:c1:92:a4 brd ff:ff:ff:ff:ff:ff
974: bradm: <BROADCAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN mode DEFAULT qlen 500
link/ether c2:15:a9:8b:db:40 brd ff:ff:ff:ff:ff:ff
976: br_t: <BROADCAST,PROMISC> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 500
link/ether ec:f4:bb:e1:0c:18 brd ff:ff:ff:ff:ff:ff
977: brpri: <BROADCAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN mode DEFAULT qlen 500
link/ether 82:f9:07:9d:d0:45 brd ff:ff:ff:ff:ff:ff
978: breip: <BROADCAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN mode DEFAULT qlen 500
link/ether 0e:7b:96:6b:22:4c brd ff:ff:ff:ff:ff:ff
如果我使用python,我可以使用re.split(),但我不知道如何做shell,谢谢〜
答案 0 :(得分:0)
在CentOS 7上:
$ source /etc/sysconfig/network-scripts/network-functions && get_device_by_hwaddr 12:34:56:9d:5b:0a
ens160
get_device_by_hwaddr()函数的来源:
$ grep get_device_by_hwaddr -A 3 /etc/sysconfig/network-scripts/network-functions
get_device_by_hwaddr ()
{
LANG=C ip -o link | awk -F ': ' -vIGNORECASE=1 '!/link\/ieee802\.11/ && /'"$1"'/ { print $2 }'
}