How do I programmatically and reliably get the AirPort's interface name?
Solution 1:
If you need to support old versions of OS X, the Wi-Fi network service was called Airport in 10.6 and earlier.
networksetup -listallhardwareports | awk '/^Hardware Port: (Wi-Fi|AirPort)$/{getline;print $2}'
You could also use -listnetworkserviceorder
:
networksetup -listnetworkserviceorder | sed -En 's/^\(Hardware Port: (Wi-Fi|AirPort), Device: (en.)\)$/\2/p'
Or read /Library/Preferences/SystemConfiguration/NetworkInterfaces.plist
:
ruby -e 'require "plist";puts Plist::parse_xml("/Library/Preferences/SystemConfiguration/NetworkInterfaces.plist")["Interfaces"].select{|i|i["SCNetworkInterfaceType"]=="IEEE80211"}[0]["BSD Name"]'
Solution 2:
How about
/usr/sbin/networksetup -listnetworkserviceorder | grep -i 'Wi-Fi\|AirPort' | grep -iow en.
Solution 3:
Playing on Lauri's theme that Apple may change the name of the Wi-Fi devices at some point in the future:
for d in `networksetup -listallhardwareports | awk '/^Device:/{print $2}'`; do
networksetup -getairportpower $d > /dev/null 2>&1 && echo "Wi-Fi Device: ${d}"
done
Since 'networksetup -getairportpower dev' throws a non-zero return value when dev is not an airport device, we can find the one(s) that work(s).