5

I need the en0, en1, enN, enWhatever name of my Airport in a script.

How do I get it reliably?

Here is what I put together, but it looks ugly and brittle as it's "text parsing":

airport_hardware_name='Hardware Port: Wi-Fi'
networksetup -listallhardwareports | awk -v p="$airport_hardware_name" '$0 ~ p { getline; print $2; }'

5 Answers 5

5

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"]'

4
  • upvoting for Snow Leopard compat! nice touch! May 5, 2013 at 15:10
  • → Lauri: 2nd version doesn't work if the wireless interface is off.
    – dan
    May 15, 2013 at 16:23
  • @danielAzuelos Do you mean like when Wi-Fi is turned off from System Preferences? It worked for me even after I turned off Wi-Fi and restarted.
    – Lri
    May 16, 2013 at 12:43
  • 1
    → Lauri: I meant when I'm using a dedicated location where there is only Ethernet on & defined. This is a configuration I use everyday to analyse network problems.
    – dan
    May 16, 2013 at 19:20
1

How about

/usr/sbin/networksetup -listnetworkserviceorder | grep -i 'Wi-Fi\|AirPort' | grep -iow en.
2
  • fails for en10, for example May 5, 2013 at 15:09
  • Don't think I have ever seen a en10. But good to know
    – markhunte
    May 6, 2013 at 19:37
1

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

1
  • This is the best so far.. built-in additional reliability!! Great stuff.. May 6, 2013 at 14:55
1

Solely to round out the answers (not to suggest that there's something wrong with the other answers, but only to show there is another way to do it), I'll throw out this:

Use the airport command found at:

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport

which has been there since at least 10.6. (I always link it to /usr/local/bin/ whenever I am setting up a new Mac).

One of the benefits of the airport command is that you don't have to know what the interface is, because:

"If an interface is not specified, airport will use the first AirPort interface on the system."

Therefore, if I run this command:

airport prefs 2>&1

I get this output:

AirPort preferences for en1:

DisconnectOnLogout=NO
Unable to retrieve JoinMode
Unable to retrieve JoinModeFallback
RememberRecentNetworks=NO
RequireAdminIBSS=NO
RequireAdminNetworkChange=NO
RequireAdminPowerToggle=NO
WoWEnabled=YES

which means that I can get the port/device name by looking for the last word on the first line, minus the colon.

airport prefs 2>&1 | awk -F' ' '/for/{print $NF}' | tr -d ':'
0

Just accidentally found another way to do this:

networksetup -setairportpower enX off 2>&1 | awk -F' ' '/:/{print $NF}'

How/why this works:

networksetup -setairportpower enX off

gives this result:

enX is not a Wi-Fi interface.

Turning off the only airport interface found: en1

Not sure that I would rank it high in terms of reliability, but I'll include it here as an option.

(Works on 10.8.3.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .