Determine the wireless network name from command-line in Linux
Running iwgetid you get the wireless device and the SSID of the connected network. It's part of the wireless-tools - that you appear to have already installed because iwevent is also part of it.
iwlist scan
should certainly work to give you the ssid. However from your example I don't see any actual wireless interfaces. I suggest pasting the output of iwconfig and ifconfig into your question to help figure that out.
Also from looking at the iwevent
man page, I think perhaps your best bet is to run a loop that calls iwevent instead. When the ssid changes, iwevent will detect and report the new ssid.
Note that polling every 5 seconds sounds pretty intensive, perhaps you should run it less frequently?
I have an old script that handles a check like this using iwconfig that's worked for several years now across several Fedora distributions:
iwconfig wlan0 | grep ESSID | awk -F: '{print $2}'Then I just make a decision in the script based on what that returns, like start up a home service or a work service. Does your iwconfig command not return info like that once associated with an access point?
using iw
Assuming that your interface is wlp3s0
, you can get the SSID with iw
and grep
:
iw wlp3s0 info | grep -Po '(?<=ssid ).*'
The flags -P
and -o
are explained in this answer.