从ifconfig的输出中提取MAC地址的最佳方法是什么?

63
从ifconfig的输出中提取MAC地址的最佳方法是什么?
示例输出:
bash-3.00# ifconfig eth0        
eth0      Link encap:Ethernet  HWaddr 1F:2E:19:10:3B:52    
          inet addr:127.0.0.66  Bcast:127.255.255.255  Mask:255.0.0.0    
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          ....
          ....

我应该使用cut、AWK还是其他什么工具?每种方法的优缺点是什么呢?


哪一个是 MAC 地址? - Nabin
@Nabin:指的是“HWaddr”字段。 - Peter Mortensen
19个回答

118

你可以在 /sys/class/ 下操作cat命令

cat /sys/class/net/*/address

特别针对 eth0

cat /sys/class/net/eth0/address

1
这会给出所有地址,不仅仅是eth0的地址,例如。 - MickeyfAgain_BeforeExitOfSO
在OpenWrt中,使用cat /sys/class/net/eth0/address非常方便: "cat /sys/class/net/eth0/address" 将返回 "c2:ee:1e:08:e6:39"。请注意,eth0将链接到硬件特定位置:"cd /sys/class/net/eth0/" 将当前工作目录更改为 "/sys/devices/platform/ag71xx.0/net/eth0/"。 - NULL pointer
1
将以下代码存储到变量中:MacAdd=\cat /sys/class/net/eth0/address``。 - haccks

74

我会使用:

ifconfig eth0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'

-o参数会让grep仅打印与表达式匹配的行的部分。 [[:xdigit:]]{1,2}将匹配1或2个十六进制数字(Solaris不输出前导零)。


1
什么?这将匹配MAC地址的任何位置,而其他解决方案则不会。 - Robert Gamble
这是我的本意,我已经使用过很多不同的Unix系统,而这是唯一适用于所有系统的解决方案。 - Robert Gamble
3
“grep -o”对我的知识也是一个方便的补充。 - paxdiablo
这是我见过的最好的方式。因为这个命令不仅在 ifconfig 上有用,而且还可以与任何其他 MAC 源一起使用,比如通过 xm network-list 提取 Xen DomU 的 MACS,ARP caché 的 MACS等。 - Álvaro
1
我使用了 ifconfig eth0 | grep -Eo '([[:xdigit:]]{1,2}[:-]){5}[[:xdigit:]]{1,2}' | head -n1 来处理那些包含 '-' 而不是 ':',或者报告超过6个字节的设备。 - Darcara
显示剩余3条评论

25

我喜欢使用/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

并非所有的发行版都提供 /sbin/ip,这个命令属于 iproute2 包。但这是最好的解决方案:ipifconfig 更加优秀! - ephemient

10

不确定是否真的有任何优点,但您可以简单地使用awk:

ifconfig eth0 | awk '/HWaddr/ {print $5}'

使用 ip addr 代替 ifconfig。ip addr | grep -C1 "link/ether" 即可。 - nPcomp

4

由于原始问题涉及到Bash,这里提供了一种无需使用其他工具即可提取HWaddr等字段的方法:

x=$(ifconfig eth0) && x=${x#*HWaddr } && echo ${x%% *}

在第一步中,它将ifconfig的输出分配给x。第二步删除“HWaddr ”之前的所有内容。在最后一步中,删除“”(MAC后面的空格)之后的所有内容。
参考:http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

3

对于Ubuntu/Debian

ifconfig | grep HW | awk '{print $5}'

对于Rhat或CentOs,请尝试使用以下命令:

ip add | grep link/ether | awk '{print $2}'

2

我更喜欢这里描述的方法(稍作修改):http://www.askdavetaylor.com/how_do_i_figure_out_my_ip_address_on_a_mac.html


该链接提供了一种简单的方法来查找Mac上的IP地址。您可以按照步骤操作,以便快速找到您的IP地址。
ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d " " -f2

你可以将其别名为短的“myip”命令以供以后使用:

echo "alias myip=\"ifconfig | grep 'inet ' | grep -v 127.0.0.1 | cut -d ' ' -f2\"" >> ~/.bash_profile

1
这个怎么样?
ifconfig eth0 | grep -Eo ..\(\:..\){5}

更具体地说

ifconfig eth0 | grep -Eo [:0-9A-F:]{2}\(\:[:0-9A-F:]{2}\){5}

还有一个简单的

ifconfig eth0 | head -n1 | tr -s ' ' | cut -d' ' -f5`

1
在Ubuntu 14.04中的终端:

ifconfig | grep HW

1
ifconfig en1 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' 
  • 将 "en1" 替换为网卡名称 "eth0" 或直接删除 "en1" - 这是一个简单而实用的解决方案。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接