从 ifconfig 输出中提取 MAC 地址

Extracting MAC address from ifconfig output

我正在编写 #!bin/bash shell 脚本来自动化 MAC 欺骗。在我编写的脚本中,我将 ifconfig -a | grep HWaddr 的输出等效于两个不同的变量。命令 ifconfig -a | grep HWaddr returns

eth0  Link encap:Ethernet HWaddr 00:00:00:00:00:00

wlan0 Link uncap: Ethernet HWaddr 00:00:00:00:00:00

但我希望命令 return 只是 wlan0 的 MAC 地址。

尝试:

[root@linux ~]$ /sbin/ifconfig wlan0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
00:25:90:F0:3F:92

通过将 wlan0 指定为 ifconfig 的第一个参数,您告诉它您只需要有关该特定接口的信息,因此您应该只返回一行。

grep 命令然后在输出中搜索 MAC 地址并仅打印匹配的 ifconfig 输出部分。

对于你的脚本,你可以尝试 follwong:

ifconfig -a | grep HWaddr | awk '{print }'

OSX

ifconfig en1 | awk '/ether/{print }'