79

Is there any way to get the SSID of the current wireless network through a shell script on Mac OS X?

Mark Szymanski
  • 56,590
  • 24
  • 70
  • 87

3 Answers3

147

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}'
efs
  • 3
  • 3
Chetan
  • 46,743
  • 31
  • 106
  • 145
  • When I do the second command, it doesn't work properly. The SSID I am connected to has spaces in it and that command only shows the first word. – Mark Szymanski Dec 19 '10 at 00:34
  • 1
    @Mark: I've updated the second command to work for SSIDs with spaces in them as well. Hope this helps! – Chetan Dec 19 '10 at 00:53
  • 1
    Tell `awk` to use `:` as a delimiter and you'll get your whole SSID: `awk -F':' '/ SSID/ {print $2}'` – johnsyweb Dec 19 '10 at 00:54
  • 1
    @Johnsyweb: According to 802.11 standard, SSID may also contain the field separator ':' – youfu Jun 28 '13 at 02:41
  • If you have spaces at the start and at the end use this `awk -F' SSID: ' '/ SSID: / { print "x",$2,"x"}'` – A.D. Mar 07 '21 at 10:39
48

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
ryenus
  • 15,711
  • 5
  • 56
  • 63
Sacrilicious
  • 1,051
  • 9
  • 13
  • 12
    Note Johnsyweb's comment above re: awk; instead of using `cut` you could run `networksetup -getairportnetwork en1 | awk -F": " '{print $2}'`, which will work regardless of whether the label contains "Airport" or "Wi-Fi". – larsks Feb 05 '12 at 23:14
  • 5
    Downside here is that you have to know your wlan interface name. I have seen e1 on macs that have a built-in ethernet (assigned e0) making the WiFi interface `e1` which seems to be the case in this answer. Others have `e0`. This is an easy edit of course but if you try to make it generic the answer by @Chetan seems easier – nhed Dec 10 '14 at 14:50
  • Or, use: networksetup -getairportnetwork en1 | cut -d':' -f2 | cut -c 2- – RichS Apr 16 '15 at 07:56
  • 2
    `networksetup -getairportnetwork en1 | cut -d ' ' -f 4` works fine, it doesn't depend on the OS version – aymericbeaumet Jun 07 '15 at 21:10
  • 1
    Or even better with "networksetup -getairportnetwork en1 | awk '{print $NF}' " there is no need to count since it always gets the last field (while separated by spaces). – Michele Dall'Agata Apr 11 '16 at 13:15
  • 1
    This should be the accepted answer as the accepted answer of using /System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport doesn't work when the script is run in non interactive mode. – et071385 Oct 23 '19 at 10:40
7

The following has been tested on OS X and prints the SSID without any hard-coded column widths:

system_profiler SPAirPortDataType | awk -F':' '/Current Network Information:/ {
    getline
    sub(/^ */, "")
    sub(/:$/, "")
    print
}'

Essentially, this takes the output of system_profiler SPAirPortDataType, and prints the line after "Current Network Information:" trimming leading whitespace and the trailing colon (since SSIDs can contain :s).

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 3
    `system_profiler` has a small delay on my OS X 10.8. `networksetup` needs Airport interface name. I'd prefer `airport | sed -n 's/^ *SSID: //p'` – youfu Jun 28 '13 at 08:07
  • Thanks. On macOS Sierra it's `airport --getinfo | sed -n 's/^ *SSID: //p'` – thisleejones May 16 '17 at 20:56
  • works well for apple network adapters, which isn't a PITA, unless you're using a USB nic on a macbook that has a busted network card. – ipatch Nov 23 '19 at 04:30