Bash script to display Mac Airport MAC address

此生再无相见时 提交于 2019-12-25 02:09:47

问题


I'm not good at scripting but found this site so hopefully some kind people out there can help me :)

I need a bash script to display the Airport Mac Address of machines, I think the command is..

ifconfig en1 

..which brings up the correct result in Terminal but I dont know how to utilise this.

I'm using Casper Suite in an office environment and need to get a report that display the MAC address of the wireless (airport) ports.

To show you how Casper works here's a script that someone wrote to check if a machine has an Airport card or not

#!/bin/sh

checkHasAnAirportCard=`networksetup -listallhardwareports | grep "Hardware Port: Air" | cut -c 16-`

if [ -z "$checkHasAnAirportCard" ]; then
    echo "<result>No</result>"
else
    echo "<result>Yes</result>"
fi

Thanks for your help & suggestions


回答1:


You can do it like this:

#!/bin/sh

networksetup -listallhardwareports | egrep -A 2 "(AirPort|Wi-Fi)" | grep Ethernet

Put this in a text file called e.g. airport.sh, make it executable (chmod +x airport.sh) and run it:

$ ./airport.sh 
Ethernet Address: 58:b0:35:65:7a:02

If you just want the MAC address on its own (without the "Ethernet Address: " prefix) then change the script to this:

#!/bin/sh

networksetup -listallhardwareports | egrep -A 2 "(AirPort|Wi-Fi)" | grep Ethernet | cut -f 3- -d ' '

This should then give e.g.:

$ ./airport.sh 
58:b0:35:65:7a:02



回答2:


Something like this may work for you (the meat of the script is ifconfig en1 | egrep -o "([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}"):

#!/bin/sh

IFACE=en1 ##Put your interface here if it isn't en1

MACADDRESS=`ifconfig ${IFACE} | egrep -o "([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}"`

if [ -n "$MACADDRESS" ]; then
    # Found a mac address, print it out
    echo "$MACADDRESS"
else  
    # No mac found, do something else
fi

Outputs the mac address:

[ 16:15 Jonathan@MacBookPro ~ ]$ ./macMac.sh 
62:c5:4a:8c:c2:74

The -n tests if $MACADDRESS is empty or not.
egrep -o searches for that specific regex and only returns the match, not the entire line.



来源:https://stackoverflow.com/questions/7973887/bash-script-to-display-mac-airport-mac-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!