Switching WLAN on/off from command line

I recently switched from an old DD-WRT to newest openWRT on my WR1043ND router. I like it, initial setup via LuCI was easy. But now I have a problem that I cant solve.
I want to be able to switch my SSID guest-WLAN (wlan0-1) and my SSID family-WLAN (wlan0) on and off independently from my smarthome environment (openHAB). Both use the hardware radio0. I plan to do the remote control by sending commands via ssh. I can log in via ssh. If I send

ip link set dev wlan0 down

or

ip link set dev wlan0-1 down

the correct interface is switched off. Good so far. Also a

ip link show | grep wlan0

shows the correct status, e.g. in UP:

41: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-lan state UP qlen 1000
42: wlan0-1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP qlen 1000

I switch it to on with "up" at the end of the command. But the wlan0-1 does not turn on (UP) if I send:

ip link set dev wlan0 up
ip link set dev wlan0-1 up

Why is it working for wlan0, but not for wlan0-1? Both have identical setup in LuCI. Any advice how to solve the problem?

Which command is called when I hit the Disable/Enable button in the LuCI? If I disabled/enable the SSIDs in LuCI it works as I wish. I noticed that wlan0 and wlan0-1 disappear for "ip link show", when disabled in LuCI. Is there another command called?

$ wifi --help
Usage: /sbin/wifi [config|up|down|reload|status]
enables (default), disables or configures devices not yet configured.
3 Likes

Yep, to control a specific logical interface on the same radio do the following:

  • check your wireless config with uci show wireless, note the relevant wifi-iface
  • toggle this interface with the 'disabled' option:
    e.g. uci -q set wireless.@wifi-iface[2].disabled=1 to disable or
    uci -q set wireless.@wifi-iface[2].disabled=0 to enable a specific interface.
  • Finally write the changes to flash with uci commit wireless
1 Like

Thank you @jeff and @dibdot. Both answers do not give the complete solution. The wifi down and wifi up commands switch WLAN completely. But I want to switch them independently.
The uci commands have the result that the interfaces appear to be disabled in the LuCI homepage, but they stay active even if switched off.

If I however combine both commands it will work:

uci -q set wireless.@wifi-iface[1].disabled=1
uci commit wireless
wifi down
wifi up

The disadvantage is that all the WIFI is off for a short time. :roll_eyes:

I put a small script together (call with switchwlan SSID on|off):

#!/bin/sh
Interface="$( uci show wireless | grep $1 | cut -d'=' -f1)"
if [ -z "$Interface" ]
then
  echo "SSID $1 not found."
else
  ucistring=$( echo "${Interface/ssid/disabled}" )
  case $2 in
    off|Off|OFF|0) uci -q set ${ucistring}=1 ;;
    on|On|ON|1)    uci -q set ${ucistring}=0 ;;
  esac
  uci commit wireless
  wifi down
  sleep 3s
  wifi up
fi
wifi reload

:crazy_face: stange, I tried this in the beginning, but it did not have any effect. In the meantime I had rebooted the router and now reload works too. Thank you. :blush:

Great, that it works. You can refine your wifi command with a second, undocumented parameter, e.g. use wifi reload radio1 to limit the command to the second radio only.

2 Likes

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