0

I'm trying to extract the MAC address for a wireless card then insert it into a bash script as a VAR variable with an incremental change. So..

$ ifconfig wlan1 | grep HWaddr
wlan1     Link encap:Ethernet  HWaddr XX:xx:XX:xx:XX:xx

Now I need to command line extract just the XX:xx:XX:xx:XX:xx and add it to the script with a +1 VAR . Which seems pretty straight forward if I can extract just the XX:xx:XX:xx:XX:xx from the grep above. (ie How to increment a variable in bash? )

Can anyone see a roadblock to adding a MAC address as a variable in a bash script? ie without bash thinking the MAC is a string calculation or erroneous command ?

  • Do you want a bash script to change 11:11:11:11:11:11 to 11:11:11:11:11:12 ? – SuB Dec 8 '16 at 14:23
  • If you capture the MAC address into a variable, you won't be able to just increment the variable, as the MAC address is a string. You could take the last 2 digits of the string as a different variable, then increment that, and replace the last 2 digits with it. – Arronical Dec 8 '16 at 14:25
  • 2
    Yes, please edit your question and explain what you mean by incrementing the MAC address by one. Give a real (or real-looking) example and show us how you'd like to change it. – terdon Dec 8 '16 at 14:29
2

It may be easier to read the MAC from the appropriate file within the sys filesystem. If you call your variable mymac, then:

$ mymac=$(cat /sys/class/net/wlan1/address)

Should work

$ echo "mymac"
XX:xx:XX:xx:XX:xx

It would be possible to increment the value of the last digits with a bit of variable manipulation. This will only work if they are both digits with a value of 98 or less. The following example uses an assumed MAC of 52:1c:53:8b:8f:77.

$ endpair=${mymac##*:}
$ ((endpair++))
$ newmac="${mymac%:*}:${endpair}"
$ echo "Hooray $mymac is now $newmac"
Hooray 52:1c:53:8b:8f:77 is now 52:1c:53:8b:8f:78
1

This post on Stack Overflow provides a portable solution (it'll work on Unix, generally, not just Ubuntu), and should provide resiliency if the output format of ifconfig changes (or if it's deprecated/removed and you decide to switch to a different tool).

ifconfig wlan1 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
  • It might be worth changing eth0 in your code to wlan1 as that's the interface the OP is interested in. – Arronical Dec 8 '16 at 15:10
  • Well spotted! I actually had to change it from en0 (testing on a Mac) - embarrassing I made the wrong change... – John N Dec 8 '16 at 15:12
  • Although Ubuntu often uses the consistent device naming thing these days so it could be anything like en2p0! – Arronical Dec 8 '16 at 15:19
0

You need to add more commands for extracting only the MAC Address.

I suggest you use awk and print only the last column, example:

ifconfig wlan1 | grep HWaddr | awk '{ print $5 }'

This command will show only XX:xx:XX:xx:XX:xx, awk print $5 split lines by spaces.

If you want add this result in bash variable, you need apply this command:

myVarName=$(ifconfig wlan1 | grep HWaddr | awk '{ print $5 }')

Then, you use this content with $myVarName.

echo $myVarName
XX:xx:XX:xx:XX:xx
  • 2
    You can avoid spawning grep by using a pattern in awk: myVarName=$(ifconfig wlan1 | awk ' /HWaddr/ { print $5 }' – John N Dec 8 '16 at 14:45

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.