Best way to extract MAC address from ifconfig's output?

RegexLinuxCommand Line-Interface

Regex Problem Overview


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.

Regex Solutions


Solution 1 - Regex

You can do a cat under /sys/class/

cat /sys/class/net/*/address

Specifically for eth0

cat /sys/class/net/eth0/address

Solution 2 - Regex

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

Solution 3 - Regex

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

Solution 4 - Regex

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

ifconfig eth0 | awk '/HWaddr/ {print $5}'

Solution 5 - Regex

Since the OP's example refers to Bash, here's a way to extract fields such as HWaddr without the use of additional tools:

x=$(ifconfig eth0) && x=${x#*HWaddr } && echo ${x%% *}

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

Solution 6 - Regex

For Ubuntu/Debian

ifconfig | grep HW | awk '{print $5}'

For Rhat or CentOs try

ip add | grep link/ether | awk '{print $2}'

Solution 7 - Regex

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

Solution 8 - Regex

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`

Solution 9 - Regex

On Ubuntu 14.04 in terminal

ifconfig | grep HW

Solution 10 - Regex

ifconfig en1 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' 
  • Replace "en1" with the name of the nic card "eth0" or remove altogether "en1" - easy and useful solution.

Solution 11 - Regex

Note: on OS X eth0 may not work. Use p2p0:

ifconfig p2p0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'

Solution 12 - Regex

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.

Solution 13 - Regex

ifconfig | grep -i hwaddr | cut -d ' ' -f11

Solution 14 - Regex

Nice and quick one:

ifconfig eth0 | grep HWaddr | cut -d ' ' -f 11

Solution 15 - Regex

I needed to get MAC address of the active adapter, so ended up using this command.

ifconfig -a | awk '/^[a-z]/ { iface=$1; mac=$NF; next } /inet addr:/ { print mac }' | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'

Hope it helps.

Solution 16 - Regex

ifconfig en0 | grep ether - for wired mac address

ifconfig en1 | grep ether - for wireless mac address

Solution 17 - Regex

this worked for me

ifconfig eth0 | grep -o -E ..:..:..:..:..:..

instead of eth0 you can write the interface you need it's mac address

Solution 18 - Regex

Output of ifconfig:

$ifconfig

eth0      Link encap:Ethernet  HWaddr 00:1b:fc:72:84:12
      inet addr:172.16.1.13  Bcast:172.16.1.255  Mask:255.255.255.0
      inet6 addr: fe80::21b:fcff:fe72:8412/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:638661 errors:0 dropped:20 overruns:0 frame:0
      TX packets:93858 errors:0 dropped:0 overruns:0 carrier:2
      collisions:0 txqueuelen:1000
      RX bytes:101655955 (101.6 MB)  TX bytes:42802760 (42.8 MB)
      Memory:dffc0000-e0000000

lo        Link encap:Local Loopback
      inet addr:127.0.0.1  Mask:255.0.0.0
      inet6 addr: ::1/128 Scope:Host
      UP LOOPBACK RUNNING  MTU:16436  Metric:1
      RX packets:3796 errors:0 dropped:0 overruns:0 frame:0
      TX packets:3796 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0
      RX bytes:517624 (517.6 KB)  TX bytes:517624 (517.6 KB)

The best way to extract MAC address is:

ifconfig | sed '1,1!d' | sed 's/.*HWaddr //' | sed 's/\ .*//' | sed -e 's/:/-/g' > mac_address

Solution 19 - Regex

Use:

ifconfig eth0 | grep HWaddr

or

ifconfig eth0 |grep HWaddr

This will pull just the MAC address and nothing else.

You can change your MAC address to whatever you want:

ifconfig eth0 down,
ifconfig eth0 hw ether (new MAC address),
ifconfig eth0 up

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAman JainView Question on Stackoverflow
Solution 1 - RegexMichalisView Answer on Stackoverflow
Solution 2 - RegexRobert GambleView Answer on Stackoverflow
Solution 3 - RegexJerubView Answer on Stackoverflow
Solution 4 - RegexalbertbView Answer on Stackoverflow
Solution 5 - RegexxebecheView Answer on Stackoverflow
Solution 6 - RegexnPcompView Answer on Stackoverflow
Solution 7 - RegexmanafireView Answer on Stackoverflow
Solution 8 - RegexphoxisView Answer on Stackoverflow
Solution 9 - RegexFernando_JrView Answer on Stackoverflow
Solution 10 - RegexTarun BansalView Answer on Stackoverflow
Solution 11 - RegexKyle CleggView Answer on Stackoverflow
Solution 12 - RegexCRGreenView Answer on Stackoverflow
Solution 13 - RegexAshwin LakshmananView Answer on Stackoverflow
Solution 14 - RegexHughView Answer on Stackoverflow
Solution 15 - RegexDogukanView Answer on Stackoverflow
Solution 16 - RegexSateesh PasalaView Answer on Stackoverflow
Solution 17 - RegexAdel SknView Answer on Stackoverflow
Solution 18 - RegexamarView Answer on Stackoverflow
Solution 19 - RegexyankeevaderView Answer on Stackoverflow