tags:

views:

2690

answers:

3

Hi

I want to know what is the best way to extract MAC Address from ifconfig 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, awk or anything else, and what are the merits and demerits of one method over other.

Thanks

Aman Jain

+2  A: 

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

ifconfig eth0 | awk '/HWaddr/ {print $5}'
albertb
+7  A: 

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

Robert Gamble
What? This will match regardless of placement of the MAC address whereas the other solutions will not.
Robert Gamble
It does have the advantage of also working on OSX since that ifconfig use 'ether' instead of 'HWaddr'
albertb
That was my intention, I have worked with plenty of different Unixes and this is the only solution that will work on all of them.
Robert Gamble
+1 for searching for the string you WANT, rather than the "noise" surrounding it.
Adam Liss
Why the downvote?
Robert Gamble
I don't understand the downvote, my original thought was that it's over-engineered for the job but it works and is better on slightly different output (I'd want to put it in a script so I don't have to remember the regex).
paxdiablo
That 'grep -o' is a handy addition to my knowledge as well.
paxdiablo
Are there any UNICES that don't output leading zeros for the MAC sections (necessitating "{1,2}" instead of "{2}")?
paxdiablo
@Pax, actually yes, I think Solaris does this, I'll update.
Robert Gamble
+3  A: 

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

$ 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:

$ ip link show eth0 | awk '/ether/ {print $2}'
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 -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
Jerub
Not all distros ship `/sbin/ip`, which belongs to the `iproute2` package. But this is the best solution: ip is much nicer than ifconfig!
ephemient