I'm writing a shell script that helps me to manage wireless network (WiFi) connections in a convenient manner. I want to be able to retrieve the status of a particular interface. For instance, I've successfully connected to a network using wpa_supplicant(8) and now I want to be able to tell from the script that right now I'm connected to a SSID 'XXX', or if I'm not currently connected, I want to be able to tell that as well.
Well, I think it can be reliably enough done looking at the ifconfig(8) output for a wireless interface:
So there's a combination of ssid and status fields in the output that can help. If we have a non-empty ssid, and the status is
But is there a way to easily query this two particular fields from the output? I understand that I can do it by grep(1)'ping these values and then parsing them in my script, but it seems to me a little complicated. I thought there will be an option that will allow to filter and limit the output to a particular field, but I couldn't find one in the manual page.
By the way, it seems that particularly for wireless connections there is a wpa_cli(8):
and it is CLI (can be conveniently used from the shell script).
But for whatever reason it doesn't work for me (even when the wpa_supplicant(8) is running and the connection is established):
I also tried specifying an interface - doesn't work either:
So, maybe these are two separate questions that I have (one about ifconfig(8) output, and one about wpa_cli(8) that doesn't work), but I think that it's OK if I post them together, because my original motivation is the same for both: I want to be able to easily get specific information about a (wireless, but not exclusively) connection, but the ways that are known to me right now either don't work, or work, but not in an 'elegant' manner so to speak.
Well, I think it can be reliably enough done looking at the ifconfig(8) output for a wireless interface:
Code:
$ ifconfig wlan0
wlan0: ...
...
ssid XXX channel 7 (2442 MHz 11g) bssid 5c:f4:ab:d0:58:78
...
status: associated
associated, then we're connected. If the status is no carrier, and the ssid is empty - we're not connected to anything.But is there a way to easily query this two particular fields from the output? I understand that I can do it by grep(1)'ping these values and then parsing them in my script, but it seems to me a little complicated. I thought there will be an option that will allow to filter and limit the output to a particular field, but I couldn't find one in the manual page.
By the way, it seems that particularly for wireless connections there is a wpa_cli(8):
Code:
The wpa_cli utility is a text-based frontend program for interacting with wpa_supplicant(8).
It is used to query current status, ...
But for whatever reason it doesn't work for me (even when the wpa_supplicant(8) is running and the connection is established):
Code:
# pgrep wpa_supplicant
6098
# wpa_cli
wpa_cli v2.11
Copyright (c) 2004-2024, Jouni Malinen <j@w1.fi> and contributors
This software may be distributed under the terms of the BSD license.
See README for more details.
Interactive mode
Could not connect to wpa_supplicant: (nil) - re-trying
I also tried specifying an interface - doesn't work either:
Code:
wpa_cli -i wlan0
wpa_cli v2.11
Copyright (c) 2004-2024, Jouni Malinen <j@w1.fi> and contributors
This software may be distributed under the terms of the BSD license.
See README for more details.
Interactive mode
Could not connect to wpa_supplicant: wlan0 - re-trying
So, maybe these are two separate questions that I have (one about ifconfig(8) output, and one about wpa_cli(8) that doesn't work), but I think that it's OK if I post them together, because my original motivation is the same for both: I want to be able to easily get specific information about a (wireless, but not exclusively) connection, but the ways that are known to me right now either don't work, or work, but not in an 'elegant' manner so to speak.