What is the best way to extract the MAC address from ifconfig
\'s output?
Sample output:
bash-3.00# ifconfig eth0
eth0 Link
You can do a cat under /sys/class/
cat /sys/class/net/*/address
Specifically for eth0
cat /sys/class/net/eth0/address
How about this one:
ifconfig eth0 | grep -Eo ..\(\:..\){5}
or more specifically
ifconfig eth0 | grep -Eo [:0-9A-F:]{2}\(\:[:0-9A-F:]{2}\){5}
and also a simple one
ifconfig eth0 | head -n1 | tr -s ' ' | cut -d' ' -f5`
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
ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d " " -f2
Which you can then alias to a short 'myip' command for future use:
echo "alias myip=\"ifconfig | grep 'inet ' | grep -v 127.0.0.1 | cut -d ' ' -f2\"" >> ~/.bash_profile
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).
ifconfig en1 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
This works for me on Mac OS X:
ifconfig en0 | grep -Eo ..\(\:..\){5}
So does:
ifconfig en0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
Both are variations of examples above.