OpenWrt Forum Archive

Topic: control wifi from the command line

The content of this topic has been archived on 4 May 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi,

I am very new to openwrt, so sorry for asking something trivial, but I could not find the information I need...

I need a script that stops my router to behave as an access point and connect to another wifi network instead, ideally in a way that would not change any configuration, so when the device reboots it works as an ap again.

The tool to use is probably "iw" but so far I have not figured out how to change the mode from "master" to "managed".

Can someone show me how to do that?

Many thanks!

Google "openwrt STA mode" and check out travelmate package after you do.

So you need to solve two tasks:

  1. Script to reconfigure router

  2. Initial script to return router into initial state

What is the problem with first item?

My problem at the moment is this:

The state my script will start to run is where the device acts as an ap.

If I try to change the mode to "managed" with "iw dev wlan0 set type managed" I get the error "command failed: Device or resource busy (-16)", but if I turn off wifi first via "wifi down" the wlan0 device disappears completely:

sudo iw dev wlan0 set type managed
command failed: No such device (-19)

ifconfig wlan0 up
ifconfig: SIOCGIFFLAGS: No such device

So either the device is busy and I cannot change the mode or the device does not even exist.

How can I transition from mode "master" to mode "managed" without changing any config?

iw is utility to just manage wifi devices. You need configure /etc/config/network, and maybe /etc/config/wireless Don't afraid to modify configs. We solve the first task, after that we will solve second.
Choose the item you want: https://wiki.openwrt.org/doc/howto/clientmode

(Last edited by ulmwind on 28 Jan 2017, 12:46)

The reason I do not want to change any config is

a) because I want to know how to do it (ifconfig/iwconfig/iw should be all you need)
b) because if I don't change any config I can simply pull the plug and it reverts to ap should anything go wrong

What probably is possible is turning off wifi, changing the config and turning it on again plus an init-script that ensures that after a reboot it goes into ap-mode, but that seems to be a very clumsy way to do it.

Is this really the way to do it?

What I forsee for my use-cases is that the essid of the network I want to connect to is not even known beforehand, but is found via scanning.

Ok, but firmware is not intended to modify such options "on the fly". Above there is suggestion of travelmate package, but it just automatically selects wifi network from specified list to connect. You want to change operation mode of wifi, it could be implemented only by configuration. You can use uci to perform it.

Ok, I admit I use my device in a way that is probably unusual.

If going down the config-way is the only way to do it, ok, I'll go down that route.

I grant you that the firmware is probably "not intended" to do such things on the fly, but that does not mean it is not possible, just that nobody so far seems to know how to do it.

But the wifi-stack is pretty much the same as on a linux-system, so why should it not be possible?

I'll invest a few more hours before I may be forced to throw the towel...

This seems to work:

# configure managed-mode 
uci set wireless.@wifi-iface[0].mode=sta

# apply the new configuration
/etc/init.d/network restart

Now wlan0 is in managed mode and iw can be used to configure it.

The nice thing is that without an "uci commit" no configuration-file is actually changed.

The discussion might have continued from here.