How to easily query wireless interface status from the shell script?

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:
Code:
$ ifconfig wlan0
wlan0: ...
    ...
    ssid XXX channel 7 (2442 MHz 11g) bssid 5c:f4:ab:d0:58:78
    ...
    status: associated
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 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, ...
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):
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.
 
Looks like lots of options but I can't get them to work:
wpa_cli --help

The error message don't make sense:
wpa_cli interface_list
Failed to connect to non-global ctrl_ifname: (nil) error: Inappropriate ioctl for device
I am asking for an list of ioctl. Not an ioctl.
 
I am asking for an list of ioctl. Not an ioctl.
You're asking for a list of interfaces here, not a list of ioctls. I believe, in order to obtain a list of interfaces, it ought to make an ioctl(2) request to the controlling device, and this request fails, that's what the error about.

The only thing that I can't get right now - is why isn't the controlling device created under /var/run/wpa_supplicant?..
 
I also tried specifying an interface - doesn't work either:
wpa_cli -i wlan0
wpa_cli(8):
Code:
SYNOPSIS
       wpa_cli	[-p  path_to_ctrl_sockets] [-i ifname] [-hvB] [-a action_file]
	       [-P pid_file] [-g global_ctrl] [-G ping_interval] command ...
command isn't optional for the command line mode.

(Using the command mode) what does wpa_cli -i wlan0 set say?
The command status instead would probably suffice for only the status.

Also maybe of interest: FreeBSD WiFi Development --Part 1 – Experimenting with WiFi
 
Back
Top