What is the best way to extract the MAC address from ifconfig
's output?
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, AWK or anything else, and what are the merits and demerits of one method over the other.
Not sure whether there really are any advantages, but you can simply use awk:
For Rhat or CentOs try
ip add | grep link/ether | awk '{print $2}'
How about this one:
or more specifically
and also a simple one
I needed to get MAC address of the active adapter, so ended up using this command.
Hope it helps.
I like using /sbin/ip for these kind of tasks, because it is far easier to parse:
You can trivially get the mac address from this output with awk:
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:
This works for me on Mac OS X:
So does:
Both are variations of examples above.