Get "first" active wireless interface

While working on the print-router-label.sh script (at https://github.com/richb-hanover/OpenWrtScripts/blob/main/print-router-label.sh), I wanted to let the script collect the information of one of the active wireless interfaces. (I use this to print a label on my router so that I know its address and credentials when I return to it months later. I also use this to configure my spare routers to a known state... See https://github.com/richb-hanover/OpenWrtScripts/blob/main/config-spare-router.sh)

Finding an active wireless interface turned out to be surprisingly difficult, so I just left the credentials as parameters to script.

But I know that UCI can see the SSIDs and keys for all the wireless interfaces. "All I want to do" is pick an active one and print its SSID and password on the label.

(Note: I'm fairly convinced that creating a label like this is "secure enough". If the bad guys are reading the password from your router's label, they are also stealing your TV and silverware...)

Can someone give me a script that identifies an active wireless interface? Thanks.

Update: I really want to return the SSID and Password of one of the active interfaces...

eh best I can do

root@OpenWrt:~# uci show wireless | egrep =wifi-iface$ | cut -d= -f1 | while read s; do uci -q get $s.disabled | grep -q 1 && break; id=$(uci -q get $s.ssid); key=$(uci -q get $s.key); echo "PRINTME SSID=$id PASS=$key"; done

iwinfo | head -1

The first disabled wireless interface will break the loop and information about subsequent (enabled) interfaces will not be printed. You should replace break with continue.

1 Like

You can use something like that:

ubus call network.wireless status | jsonfilter -l 1 -e '@[@.up=true].interfaces.*.config.ssid'
ubus call network.wireless status | jsonfilter -l 1 -e '@[@.up=true].interfaces.*.config.key'
1 Like

Parsing UCI the good old way ... :wink:

#!/bin/sh

# source openwrt function library
#
. "/lib/functions.sh"
first="0"

# get options per wifi-iface section
#
f_getif() {
	local _disabled _ssid _key section="$1"

	config_get _disabled "${section}" "disabled" "0"
	if [ "${_disabled}" = "0" ] && [ "${first}" = "0" ]; then
		config_get _ssid "${section}" "ssid"
		config_get _key "${section}" "key"
		if [ -n "${_ssid}" ] && [ -n "${_key}" ]; then
			first="1"
			echo "SSID: ${_ssid}, KEY: ${_key}"
		fi
	fi
}

# load wireless config
#
config_load wireless
config_foreach f_getif "wifi-iface"

Output:

root@blackhole:/tmp# ./get-wifi.sh 
SSID: blackhole.nl, KEY: Not!Safe2G#123

Doh, good catch :slight_smile:

So many useful responses:

@appelsin's response, when unwound onto multiple lines and substituting "continue" gives the FIRST active radio's credentials:

uci show wireless |\
egrep =wifi-iface$ |\
cut -d= -f1 |\
while read s;
    do uci -q get $s.disabled |\
    grep -q 1 && continue;
    id=$(uci -q get $s.ssid);
    key=$(uci -q get $s.key);
    echo "PRINTME SSID=$id PASS=$key";
    break
done

@dibdot's solution appears to give info about ALL active radios:

#!/bin/sh

# source openwrt function library
#
. "/lib/functions.sh"

# get options per wifi-iface section
#
f_getif() {
	local _disabled _ssid _key section="$1"

	config_get _disabled "${section}" "disabled" "0"
	if [ "${_disabled}" = "0" ]; then
		config_get _ssid "${section}" "ssid"
		config_get _key "${section}" "key"
		echo "SSID: ${_ssid}, KEY: ${_key}"
	fi
}

# load wireless config
#
config_load wireless
config_foreach f_getif "wifi-iface"

Thanks to all!

2 Likes

@richb-hanover-priv Sorry, I had not read the task well enough. I've edited my proposed solution and changed it to a "first only" variant .... :wink:

Thanks again for all those responses. I now have a print-router-label.sh script (OpenWrtScripts repo) that reads the config of a router, and prints the following information. I tape it to the side of my router so I can come back to it easily. (I got tired of scribbling this info on a tiny slip of paper. Now I let the computer do the work for me...)

This is part of my larger "Spare Router" project that resets decommissioned routers to a known good, easy-to-reuse state. See the config-spare-router.sh script.

Here's a sample label:

======= Printed with: print-router-label.sh =======
     Device: GL.iNet GL-MT6000
    OpenWrt: OpenWrt 23.05.5 r24106-10cc5fcd00
 Connect to: http://SpareRouter.local
         or: ssh root@SpareRouter.local
        LAN: 172.30.42.1
       User: root
   Login PW: SpareRouter
  Wifi SSID: SpareRouter
    Wifi PW: <no password>
 Configured: 2024-Nov-29
=== See github.com/richb-hanover/OpenWrtScripts ===

Label for Power Brick: GL.iNet GL-MT6000

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.