我喜欢将 /sbin/ip 用于这类任务,因为它更容易解析:
$ ip link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:30:21:48 brd ff:ff:ff:ff:ff:ff
您可以使用 awk 轻松地从此输出中获取 mac 地址:
$ ip link show eth0 | awk '/ether/ {print $2}'
00:0c:29:30:21:48
如果您想花更多的精力,并解析出更多数据,我建议在 ip 命令中使用 -online 参数,它可以让您将每一行视为一个新设备:
$ ip -o link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue \ link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000\ link/ether 00:0c:29:30:21:48 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000\ link/ether 00:0c:29:30:21:52 brd ff:ff:ff:ff:ff:ff
4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 100\ link/[65534]
5: sit0: <NOARP> mtu 1480 qdisc noop \ link/sit 0.0.0.0 brd 0.0.0.0