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.
On Ubuntu 14.04 in terminal
Note: on OS X eth0 may not work. Use p2p0:
I prefer the method described here (with slight modification): http://www.askdavetaylor.com/how_do_i_figure_out_my_ip_address_on_a_mac.html
Which you can then alias to a short 'myip' command for future use:
ifconfig en0 | grep ether - for wired mac address
ifconfig en1 | grep ether - for wireless mac address
Output of ifconfig:
The best way to extract MAC address is:
I would use:
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).