Get wireless SSID through shell script on Mac OS X

Solution 1:

The command

/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I

will give you details about your current wireless network connection.

To get specifically the SSID, use this command:

/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk -F: '/ SSID/{print $2}'

To retrieve SSID names that might have colons as well as spaces:

/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I  | awk -F' SSID: '  '/ SSID: / {print $2}'

Solution 2:

Where isn't there a wheel in need of re-inventing?

networksetup -getairportnetwork en1 | cut -c 25-

is what you'd use on 10.6, 10.7 changed the "Hardware Port" name from "Airport" to "Wi-Fi", and therefore you'd cut off one less letter,

aru$ networksetup -getairportnetwork en1 | cut -c 24-
Yorimichi

In case the device is named something other than en1, one needs to first get the correct device name, than the corresponding SSID:

networksetup -listallhardwareports | awk '/Wi-Fi/{getline; print $2}' | xargs networksetup -getairportnetwork