11

I want to obtain the currently connected wifi networks ssid in a bash script. I am trying to write a backup script where the script will perform a backup to a NAS server if it's connected to my home wifi network. I have looked into the ip route command but it only returns some basic information - enter image description here

7 Answers 7

22
iwgetid -r

or

iwgetid wlan0 -r
2
6

The following should provide what you are looking for assuming you are connected using 1 wireless device:

nmcli -t -f ssid dev wifi| cut -d\' -f2
12
  • it's returning all the connected networks. Can I find only the connected network?
    – Chan
    Apr 18, 2013 at 6:45
  • Try it like this and let me know: nmcli -t -f active,ssid dev wifi| cut -d\' -f2 Apr 18, 2013 at 6:46
  • Still returning many results
    – Chan
    Apr 18, 2013 at 6:47
  • Can you pastebin what you are seeing as output? Apr 18, 2013 at 6:48
  • 2
    This works for me: nmcli -t -f active,ssid dev wifi | grep '^yes' | cut -d: -f2 . Jun 5, 2019 at 21:34
2

This command returns the SSID of the connected wireless adapter (assuming you only have one).

iwconfig | grep ESSID | sed -e 's/.*ESSID:"\(.*\)".*/\1/'

It also print warning on the terminal but on stderr so it doesn't matter

remi@host~$id:~$ id=$(iwconfig | grep ESSID | sed -e 's/.*ESSID:"\(.*\)".*/\1/')
eth0      no wireless extensions.
lo        no wireless extensions.
virbr0    no wireless extensions.
tap0      no wireless extensions.

remi@host:~$ echo $id
CISPI
1

How 'bout iwconfig wlan0 | sed -e '/ESSID/!d' -e 's/.*ESSID:"/"/'?

1

With NetworkManager-1.8.4, this produced the correct result

LANG=C nmcli -t -f active,ssid dev wifi | grep ^yes | cut -d: -f2-

There is a reason for every part of the command

  • LANG=C is because we are using grep on localized string so force english
  • nmcli ... -f active,ssid ... causes to print ssid with active status in form yes:myssid no:otherssid
  • grep ^yes we want to filter active connections, but not SSIDs with text "yes" so it is the reason for ^
  • cut ... -f2- prints the rest of the line after the first separator so we can have SSID with separator in it
0
nmcli -t -f NAME connection show --active
  • -t Makes the output 'terse' so no headers
  • -f NAME Shows only the ssid
  • --active Shows only the active connections
1
  • This prints the NAME of connection and not the SSID. NAME usually corresponds with SSID but it is not always true. NAME of NetworkManager connection can be changed to any random value.
    – j123b567
    Jun 6, 2018 at 15:36
0

I tried this:

    iwconfig wlan0 | grep ESSID | cut -d\" -f2

You must log in to answer this question.

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