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.
You can do a cat under
/sys/class/
Specifically for
eth0
Since the OP's example refers to Bash, here's a way to extract fields such as HWaddr without the use of additional tools:
In the 1st step this assigns the ouput of ifconfig to x. The 2nd step removes everything before "HWaddr ". In the final step everything after " " (the space behind the MAC) is removed.
Reference: http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
Use:
or
This will pull just the MAC address and nothing else.
You can change your MAC address to whatever you want:
Nice and quick one: