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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/245916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:40:12  来源:igfitidea点击:

Best way to extract MAC address from ifconfig's output?

regexlinuxcommand-line-interface

提问by Aman Jain

What is the best way to extract the MAC address from ifconfig's output?

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

Sample output:

示例输出:

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
          ....
          ....

Should I use cut, AWKor anything else, and what are the merits and demerits of one method over the other.

我应该使用 cut、AWK还是其他任何方法,以及一种方法相对于另一种方法的优缺点是什么。

回答by albertb

Not sure whether there really are any advantages, but you can simply use awk:

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

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

回答by Robert Gamble

I would use:

我会用:

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

The -o will cause grep to only print the part of the line that matches the expression. [[:xdigit:]]{1,2}will match 1 or 2 hexidecimal digits (Solaris doesn't output leading zeros).

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

回答by Jerub

I like using /sbin/ip for these kind of tasks, because it is far easier to parse:

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

You can trivially get the mac address from this output with awk:

您可以使用 awk 从该输出中轻松获取 mac 地址:

$ ip link show eth0 | awk '/ether/ {print }'
00:0c:29:30:21:48

If you want to put a little more effort in, and parse more data out, I recommend using the -online argument to the ip command, which will let you treat every line as a new device:

如果你想多花点力气,解析出更多的数据,我建议在 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

回答by xebeche

Since the OP's example refers to Bash, here's a way to extract fields such as HWaddr without the use of additional tools:

由于 OP 的示例引用了 Bash,因此这里有一种无需使用其他工具即可提取 HWaddr 等字段的方法:

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

In the 1st step this assigns the ouput of ifconfig to x. The 2nd step removes everything before "HWaddr ". In the final step everything after " " (the space behind the MAC) is removed.

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

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

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

回答by phoxis

How about this one:

这个怎么样:

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

or more specifically

或更具体地说

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

and also a simple one

还有一个简单的

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

回答by Michalis

You can do a cat under /sys/class/

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

cat /sys/class/net/*/address

Specifically for eth0

专为 eth0

cat /sys/class/net/eth0/address

回答by manafire

I prefer the method described here (with slight modification): http://www.askdavetaylor.com/how_do_i_figure_out_my_ip_address_on_a_mac.html

我更喜欢这里描述的方法(稍作修改):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

Which you can then alias to a short 'myip' command for future use:

然后您可以将其别名为一个简短的“myip”命令以供将来使用:

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

回答by Kyle Clegg

Note: on OS X eth0 may not work. Use p2p0:

注意:在 OS X eth0 上可能不起作用。使用 p2p0:

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

回答by CRGreen

This works for me on Mac OS X:

这在 Mac OS X 上对我有用:

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

So does:

也是如此:

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

Both are variations of examples above.

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

回答by amar

Output of ifconfig:

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)

The best way to extract MAC address is:

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

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