1

I am writing a #!bin/bash shell script to automate MAC spoofing. In the script that I have written I make the outputs of ifconfig -a | grep HWaddr equivalent to two different variables. The command ifconfig -a | grep HWaddr returns

eth0  Link encap:Ethernet HWaddr 00:00:00:00:00:00

and

wlan0 Link uncap: Ethernet HWaddr 00:00:00:00:00:00

but I want the command to return just the MAC address for wlan0.

Eric
  • 893
  • 10
  • 25
  • `ifconfig` is deprecated, use `ip`. – Cyrus Apr 20 '15 at 17:48
  • The command `ifconfig` is not deprecated in MAC OS? Is the command deprecated in all Linux distributions or just a few? – Eric Apr 20 '15 at 17:56
  • @Eric: For Linux, `ifconfig` is considered deprecated, but it doesn't do you a lot of harm to use it. It is highly unlikely to disappear any time soon. See http://serverfault.com/questions/458628/should-i-quit-using-ifconfig – Conspicuous Compiler Apr 20 '15 at 17:57
  • @Eric: For OS X, you're right, `ifconfig` is still the preferred tool, but it is a significantly different beast than Linux's `ifconfig` and is, in fact, closer to Linux's `ip` in functionality. – Conspicuous Compiler Apr 20 '15 at 17:58
  • https://lists.debian.org/debian-devel/2009/03/msg00780.html – Eric Apr 20 '15 at 18:00
  • 1
    I agree but I am shooting for cross-compatibility for all things *nix, whether it be Linux or Unix. – Eric Apr 20 '15 at 18:01
  • @Eric: I was referring to Linux. – Cyrus Apr 20 '15 at 18:04
  • 1
    Your question is tagged with Linux and not Mac OS. – Cyrus Apr 20 '15 at 18:10
  • @Cyrus `+1 Upvote` agreed, if question is tag for `linux` then why people talking about `OSX` and other flavor – Satish Apr 20 '15 at 18:14
  • 1
    I just updated the tag. – Eric Apr 20 '15 at 18:14
  • @Eric if you looking for all platform run-able script then you have to first detect `OS` and base on that run specific function or command to get `MAC` – Satish Apr 20 '15 at 18:19

1 Answers1

6

Try:

[root@linux ~]$ /sbin/ifconfig wlan0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
00:25:90:F0:3F:92

By specifying wlan0 as the first argument to ifconfig, you are telling it you only want information about that particular interface, so you should only get a single line returned.

The grep command then searches for a MAC address in the output and prints only the portion of the ifconfig output which matches.

OR

Just for your script you can try follwong:

ifconfig -a | grep HWaddr | awk '{print $5}'

OSX

ifconfig en1 | awk '/ether/{print $2}'
Satish
  • 16,544
  • 29
  • 93
  • 149