Extracting MAC address from ifconfig output

5.6k views Asked by At

I am writing a #!bin/bash shell script to automate MAC spoofing. In the script that I have written I make the outputs of ifconfig -a | grep HWaddr equivalent to two different variables. The command ifconfig -a | grep HWaddr returns

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

and

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

but I want the command to return just the MAC address for wlan0.

1

There are 1 answers

7
Satish On BEST ANSWER

Try:

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

By specifying wlan0 as the first argument to ifconfig, you are telling it you only want information about that particular interface, so you should only get a single line returned.

The grep command then searches for a MAC address in the output and prints only the portion of the ifconfig output which matches.

OR

Just for your script you can try follwong:

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

OSX

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