从ifconfig输出中提取MAC地址

Eri*_*ric 0 linux macos bash ifconfig mac-spoofing

我正在编写一个#!bin/bashshell脚本来自动化MAC欺骗.在我编写的脚本中,我将输出ifconfig -a | grep HWaddr相当于两个不同的变量.该命令ifconfig -a | grep HWaddr返回

eth0  Link encap:Ethernet HWaddr 00:00:00:00:00:00
Run Code Online (Sandbox Code Playgroud)

wlan0 Link uncap: Ethernet HWaddr 00:00:00:00:00:00
Run Code Online (Sandbox Code Playgroud)

但我希望命令返回刚才的MAC地址wlan0.

Sat*_*ish 5

尝试:

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

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

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

要么

只为您的脚本,您可以尝试以下:

ifconfig -a | grep HWaddr | awk '{print $5}'
Run Code Online (Sandbox Code Playgroud)

OSX

ifconfig en1 | awk '/ether/{print $2}'
Run Code Online (Sandbox Code Playgroud)