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 -

Asked
Active
Viewed 2.1k times
11
Chan
- 343
- 1
- 3
- 13
7 Answers
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
Luis Alvarado
- 209,003
- 167
- 543
- 707
-
it's returning all the connected networks. Can I find only the connected network? – Chan Apr 18 '13 at 06:45
-
Try it like this and let me know: nmcli -t -f active,ssid dev wifi| cut -d\' -f2 – Luis Alvarado Apr 18 '13 at 06:46
-
-
-
-
@j123b567 Hi, I just had time to test this again and it works correctly. Both of them actually. Tested on Ubuntu 18.04 – Luis Alvarado Jun 10 '18 at 15:35
-
@LuisAlvarado can you please post your output of `nmcli dev wifi` and `env -i bash --noprofile --norc -c "nmcli dev wifi"`? – j123b567 Jun 11 '18 at 13:24
-
-
@LuisAlvarado mine is similar. So how can `|grep yes| cut -d\' -f2` works with this output? – j123b567 Jun 12 '18 at 16:52
-
@j123b567 yes I see what you mean, the other one is totally broken. Most likely at the moment it was working before and then after an update it broke. I will remove that one. Thank you buddy – Luis Alvarado Jun 12 '18 at 22:15
-
2This works for me: `nmcli -t -f active,ssid dev wifi | grep '^yes' | cut -d: -f2 `. – Violet Shreve Jun 05 '19 at 21:34
-
@JacobEvanShreve Yes that one also works nicely. Thank you friend. – Luis Alvarado Jun 05 '19 at 22:01
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
Rémi
- 945
- 8
- 15
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=Cis because we are using grep on localized string so force englishnmcli ... -f active,ssid ...causes to print ssid with active status in formyes:myssidno:otherssidgrep ^yeswe 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
j123b567
- 166
- 5
0
nmcli -t -f NAME connection show --active
-tMakes the output 'terse' so no headers-f NAMEShows only the ssid--activeShows only the active connections
muru
- 193,181
- 53
- 473
- 722
John Mehorter
- 11
- 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 06 '18 at 15:36