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

问题描述 投票:61回答:17

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还是其他任何东西,以及一种方法相对于另一种方法的优点和缺点。

regex linux command-line-interface
17个回答
106
投票

你可以在/sys/class/下做一只猫

cat /sys/class/net/*/address

特别是对于eth0

cat /sys/class/net/eth0/address

0
投票

注意:在OS X上,eth0可能无法正常工作。使用p2p0:

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

0
投票

这适用于Mac OS X:

ifconfig en0 | grep -Eo ..\(\:..\){5}

那样做:

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

两者都是上述示例的变体。


0
投票

好又快的一个:

ifconfig eth0 | grep HWaddr | cut -d ' ' -f 11

0
投票
ifconfig | grep -i hwaddr | cut -d ' ' -f11

0
投票

我需要获取活动适配器的MAC地址,因此最终使用此命令。

ifconfig -a | awk '/^[a-z]/ { iface=$1; mac=$NF; next } /inet addr:/ { print mac }' | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'

希望能帮助到你。


0
投票

ifconfig en0 | grep ether - 用于有线mac地址

ifconfig en1 | grep ether - 用于无线mac地址


-1
投票

ifconfig的输出:

$ifconfig

eth0      Link encap:Ethernet  HWaddr 00:1b:fc:72:84:12
      inet addr:172.16.1.13  Bcast:172.16.1.255  Mask:255.255.255.0
      inet6 addr: fe80::21b:fcff:fe72:8412/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:638661 errors:0 dropped:20 overruns:0 frame:0
      TX packets:93858 errors:0 dropped:0 overruns:0 carrier:2
      collisions:0 txqueuelen:1000
      RX bytes:101655955 (101.6 MB)  TX bytes:42802760 (42.8 MB)
      Memory:dffc0000-e0000000

lo        Link encap:Local Loopback
      inet addr:127.0.0.1  Mask:255.0.0.0
      inet6 addr: ::1/128 Scope:Host
      UP LOOPBACK RUNNING  MTU:16436  Metric:1
      RX packets:3796 errors:0 dropped:0 overruns:0 frame:0
      TX packets:3796 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0
      RX bytes:517624 (517.6 KB)  TX bytes:517624 (517.6 KB)

提取MAC地址的最佳方法是:

ifconfig | sed '1,1!d' | sed 's/.*HWaddr //' | sed 's/\ .*//' | sed -e 's/:/-/g' > mac_address

-1
投票

使用:

ifconfig eth0 | grep HWaddr

要么

ifconfig eth0 |grep HWaddr

这将只提取MAC地址,而不是其他任何内容。

您可以将MAC地址更改为您想要的任何内容:

ifconfig eth0 down,
ifconfig eth0 hw ether (new MAC address),
ifconfig eth0 up

67
投票

我会用:

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

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


22
投票

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

10
投票

不确定是否确实有任何优势,但你可以简单地使用awk:

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

4
投票

由于OP的示例引用了Bash,因此这里是一种在不使用其他工具的情况下提取HWaddr等字段的方法:

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

在第1步中,将ifconfig的输出分配给x。第二步删除“HWaddr”之前的所有内容。在最后一步中,“”(MAC后面的空间)之后的所有内容都将被删除。

参考:http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion


2
投票

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

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

2
投票
ifconfig | grep HW | awk '{print $5}'

对于Rhat或CentOs,请尝试ip add | grep link/ether | awk '{print $2}'


1
投票

在终端上的Ubuntu 14.04上

ifconfig | grep HW

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`
© www.soinside.com 2019 - 2024. All rights reserved.