OpenWrt Forum Archive

Topic: [Howto](AAP) Automated Wifi network change if the current fails

The content of this topic has been archived between 1 Sep 2014 and 5 May 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Based on my needs, I had to find a solution to make the router change the wireless network if it went down, by itself.
After some work (I'm kinda nooby with linux scripting), I got this script.
It's been growing slowly with the help of the users in the forum, actually it works very well.
Many thanks to them!

Requisites:
- Openwrt installed, will not work on DDWRT!
-  Enable and configure wifi as client, set it to WAN:

uci set wireless.@wifi-iface[0].disabled=0
uci set wireless.@wifi-iface[0].mode=sta
uci set wireless.@wifi-iface[0].network=wan

Disable wan from physical wired port, so it will work with wifi:

uci del network.wan.ifname
uci del network.wan6.ifname

         


Download the zip file, and follow the intructions inside "Readme.txt"
Do not copy & paste the text! It may fail due differences between linux's and windows's new line syntax.
There are two versions:

-  WifiMgr 1.0 switches the wifi network ONLY when the network fails

-  WifiMgr 1.0b also checks every few seconds if the preferred network is available, and switches to it if it's not connected to it already. This behaviour will cause constant drops if the preferred network fails often, as it will be switching constantly!

https://mega.nz/#F!KUcVSb6L!UvNxZlm1VsiyFAW3n6wAzA


To modify networks, edit the file /etc/wifiMgr/config.
Remember that if "net1" (COFFEE1) is found, it will be configured before "net2"(DSLrouter).
So, the order is important, configure at your own needs.
/etc/wifiMgr/config

##################################
#    wifiMgr config file
##################################

# Background internet connection checking interval
ConnCheckTimer=60

# After new network is set, time to wait for network to establish, before checking if it's working
NewConnCheckTimer=25

# Set a random MAC on each boot
randMac="0"


net1_ssid="COFFEE1"
net1_encrypt="wep"
net1_key="CofeeWireless"

net2_ssid="DSLrouter"
net2_encrypt="psk2"
net2_key="dsl12345678"

net3_ssid="Home"
net3_encrypt="psk"
net3_key="MyHomeNetwork"

# ....and so on, make as much as you want.  Remember to put the values between "" !

NOTE:
The script will never do anything else than a few "pings" when the network is ok.
Continuosly scanning and going to the preferred network would cause problems, like broken downloads or angry gamers.
So, it only works when the connection is already down!


                                                                                      UPDATES

2013-09-12
* Added input arguments to the script.
   Now you can force a network scan and reconfigure the network by issuing:
            wifiMgr.sh --force

   The "--daemon" flag is created but shouldn't be used directly by the user. It is reserved for the init.d startup script.

2013-09-13
*   Added timing variables.
*   Changed "wlist" to a better undestandable name: "config"
*   Now you can force a network scan specifying a network, example:
            wifiMgr.sh --force 3

    It will try to connect to the corresponding network "net3_ssid" (Home) and the result:
        Not found, connection failed, or sucessful

2013-09-29
*   Now it performs a scan on each reboot and reverts to the preferred network in the config file.

2013-10-08
*   Added the variable "randMac". Setting it to "1" inside the config file will make the script to set a random MAC on each boot

2013-10-16
*   Now all the generated MAC addresses have the format 00:xx:xx:xx:xx:02.
    This is for testing purposes, as there was some strange issues on some of the generated MAC addresses

2014-06-03
*   Fixed the file, it was completely wrong, it had a mix of files from an old version and the newest, so it didn't work.
     Sorry for the big mistake!

(Last edited by dabyd64 on 22 Apr 2016, 20:56)

Hi  dabyd64,

If all 3 remote access points are down, then your router would go into a loop, right?

BTW, did you get the problem as I mentioned in the post https://forum.openwrt.org/viewtopic.php?id=44040 ?
Which version of openwrt you are using?

Thanks!

Yes, if none of them are available it would loop until it finds any.
The repeater mode got that limitation. If the client network can't connect , the AP network breaks.

wow! is this a sort of AAP ??

What do you mean by "AAP" ?

Oh, I love that page too. Useful sometimes lol
So it was already done? Damn! I asked and nobody told me.. -.- so I did mine

the point is, there was NO script like yours! because AFAIK AAP was never made to work with newer kernels and ath9k drivers!

Then it was time for something like this to appear smile.
Usually,  I had to go to a friend's house, who depends on (free) coffee shops wifi networks, and explain how to change the wireless config if any went down. He said it was easy, but after two days he called again.
So I did this basically to avoid headaches lol , then I saw that it worked great, so I shared there!
As you can see, this only depends on uci and iw commands, so as long as they exist its shoud work on any kernel version...I think.
I don't know about Broadcom and Ralink chipsets, so I got no idea if it works for them, I imagine it would need some small modifications.

(Last edited by dabyd64 on 14 May 2013, 20:36)

Could this be easily adjusted, to provide auto-scan / auto-connection to ANY open / known network?

At least so far as I can tell, that seems to be the major difference between this and other AAP type scripts...  With this one, it seems you've got to enter each of the permitted networks into the configs.  With others, you can enter known closed networks or auto-connect to any open ones, and quite seamlessly...

Nope, this only works with pre-configured networks.
Where did you see other scripts that allow automatic connection to any open network?
It sound interesting smile

I wonder whether the three APs you are connecting to are using the same channel or not? If not, then what is your configured channel on the client router?

I've been trying your script since yesterday, and wifi connections on my client router got dropped many times today (ath: Failed to stop TX DMA, queues=0x004). Had to restart, and monitoring

Are you using latest trunk? I got a lot of issues with it.
I went back to Attitude Adjustment (12.09 final) and all went fine again.
About the channel, it doesn't care. I can put whatever, that it will change to the correct one automatically.

(Last edited by dabyd64 on 24 Jun 2013, 01:56)

dabyd64 wrote:

Are you using latest trunk? I got a lot of issues with it.
I went back to Attitude Adjustment (12.09 final) and all went fine again.
About the channel, it doesn't care. I can put whatever, that it will change to the correct one automatically.

Hi, dabyd64,

Good to know you have written this script.
Since Orange has no plan for AAP v4, this seems the best for my needs.

What shall be changed if this script works with the latest DD-wrt, atheros, usually new kernel 3.6.

https://forum.openwrt.org/viewtopic.php?id=43991

(Last edited by eeff11 on 5 Jul 2013, 10:57)

Almost all...this script depends on UCI , as far as I know ddwrt has no uci, and I don't know how it stores the config.
Anyway, ddwrt is very unstable on atheros hardware, and has a lot of bugs. Since I put openwrt 12.09 final, 35 days on without rebooting, and going up!

dabyd64 wrote:

Almost all...this script depends on UCI , as far as I know ddwrt has no uci, and I don't know how it stores the config.
Anyway, ddwrt is very unstable on atheros hardware, and has a lot of bugs. Since I put openwrt 12.09 final, 35 days on without rebooting, and going up!

Well, quite the contrary, DD-wrt for atheros is quite stable for client repeater, several weeks no need to reboot. Do have a lot of bugs, but they update quite often.

DD-wrt is more user-friendly and openwrt seems more developer-friendly.

I have both. But for use, I prefer DD.

Hope some script guru can develop autoap scripts for atheros DD-WRT.

In DD-WRT forum, no one responds with my wish.
In openwrt, there are reposponses because there are more developers like you, dabyd64 or who can do sth rather than just “flash" the router and start to use.

Need autoap script for DD-WRT, the latest firmware with new kernel like 3.6.

Thanks for your script.

Well... I TRIED to use ddwrt... but they update every 2-3 months and usually no great fixes.
And they have a very serious issues like WEP not working since a lot of time ago (>1year).
At least this is what happened with the TpLink 740/741 v4...I hope other router got better luck!

just to tell you I prefer using this code to check for network connectivity even if it use awk:

gtw=`netstat -rn | grep 0.0.0.0 | awk '{print $2}' | grep -v "0.0.0.0"`
if  ping $gtw -c 1 > /dev/null
then
      echo OK
else
      count=$(expr "$count" + "1")
fi

with many access point on the same network, if one is unconnected it is skipped

this is not yet tested, and waiting for your comments.

the other part of your script is a god sent gift, thanks a lot

(Last edited by viknet on 19 Jul 2013, 23:28)

Hi viknet. I've been very busy lately.
Thanks for the code, it seems a good way to check the network status.
I'll take a look on it

New script:

Easier, more stable, clean and simple.Check post #1

(Last edited by dabyd64 on 13 Aug 2013, 00:05)

Hi, dabyd64,

First,thanks so much for your script,still have some questions.

1.Can it be used in backfire 10.3.1?
2.Suppose,booted the router, then successfully connected to  "COFEE1" and after some while "COFEE1" shutdown,what will happen with your script? I used autoap in dd-wrt for Broadcom, it will auto connect to the next one, in your example "DSLrouter",if "COFFE1" a.SHUT DOWN b.Changed password c. no internet connectivity, and it goes on the white list, can set the ping time for google.com,say each 1 minute, sure if it's OK,then no disturbing with the connection with the "COFFE1",if not OK, then try connecting to "DSLrouter",and on and on...
3. What shall be added in the script for initialising the MacChanger.ipk each booting of the router.

Thanks again for the script, I am still figuring out orange's AAP, he just seldom shows up. Before I learn to compile and upgrade to Attitude Adjustment with my tp-link 840n v1,I may need your script to run on backfire already flashed.

Thanks.

I've added some better connection checks to the script.



1
About if it will work on Backfire, no idea.
No special kernel needs, it works with the stock openwrt AA 12.09.
So I suppose that yes, it could work. Try it and tell me if it works.

Of course, this is working on Atheros. No idea about Broadcom or Ralink devices.


2
The script does ping Google every 30 seconds.
If suddenly the connection is lost, it detects that condition, it makes a wireless survey, then it searches the availability of the pre-configured networks inside "wlist" file.
It configures the first wifi network available that it finds inside of the wlist file.
After applying the new config, it wait 10 seconds, then it pings Google again.
If it got response, it returns to the main loop (checking the network status periodically).
If it didn't, it will discard that network and jump to the next in the list and try again.

If you are connected to "COFFEE1" and it powers off, it will detect that it not longer has connectivity.
Then it will scan for the wireless networks available and, check if "COFFEE1" exists,
As it will not appear, it will check the next network, "DSLrouter". If it exists, it will set the ssid, security and key, as set on the wlist file.
If COFFEE1 reappears, it will not do anything to check if it exists, unless the wireless drops again.

If the wireless network changes it's name, then it will be treated like a different network, so the router will not see it again unless the wlist file is re-configured.
If it changes the security... No problem, remember: After changing the network, it waits 10 seconds, then sends a few pings to Google.
If it got no response, it will discard the changes and continue searching the next wireless network available, until it finds a network that is available and also has internet.

My knowledge in linux scripting is like the worst of any of you.
I did this by searching and learning a bit on google!
If anyone with more knowledge wants to make it better, (s)he's free to do that.
But I don't know how to do , and I won't spend hours on searching how make blacklist/whitelist, key change detection, or anything else.
That would end in a mess.
This was a personal need, I shared here for everyone because it worked well for me , but don't expect much more lol



3
After installing macchanger, add the macchanger command after the main loop, as this:

macchanger wlan0  -r
while [ "1" ]; do
        NetStatus
        sleep 15
done

Then it will set the mac randomly each boot

(Last edited by dabyd64 on 18 Aug 2013, 03:12)

Hi, dabyd64,

Thanks so much for your quick response and the scripts,your time and efforts.

I will test it and report to you.

One thing is that the script use essid to detect rather than bssid, some users just too lazy that after scan there are several "MyBuffalo","MyNetgear", then I don't know what will the script does.My suggestion is that by combining these two, essid+bssid, that is setting them both in the white list, that every wireless signal is UNIQUE, can connect to them as in the prefered order.Sure, if they change either the essid or bssid, the script will treat them as different routers.

I am good at designing websites, some ERP programming, but worse than you at the scripting.

And I am learning further how to develop a gui web, just put an addtional tab on the LUCI web interface "autoplay", with autoap, aircrack, reaver,MDK3 and screen, just input the essid&bssid in the input text field, radio button, check box,etc.

Choose it in two modes"Learning mode" or "connecting Mode", auto reaver from the strongest "pinnable“ signal and keep the log once deadlocked or finish and then reaver the next until final. Or simply auto connect in the prefered list.

Seems a long road to go, first have to master PHP or something similar.

Thanks, I will test it first.

PunBB bbcode test

PunBB bbcode test

That sounds interesting! I have zero knowledge about PHP... lol

Yes, I thought on searching for the BSSID, but I don't know how to parse the "iw scan" output to ensure that both ssid and bssid are from the same network...unless you suppose that if "x" bssid and "y" ssid appear, they are from the same network.
If there are a few networks with the same name, "WIFI" and the configured the network "WIFI" with bssid "00:11:22:33:44:55" changes its name to "WIFI2", the iw scan will show "WIFI" and "WIFI2" ssids.
With a simple grep the script will think that the network still exists.
¿How to ensure that it won't happen?
Starting by adding a new variable inside the wlist file, called net(x)_bssid="00:11:22:33:44:55".
Then, running iw wlan0 scan | grep -e "BSS " -e "SSID: " will show something like this:

BSS 00:18:46:73:04:52 (on wlan0)
        SSID: WIRELESS
BSS 00:14:25:D9:b1:3C (on wlan0)
        SSID: HOME
                 * OBSS non-GF present: 0
BSS 00:91:52:32:c2:20 (on wlan0)
        SSID: WLAN
BSS 00:22:3f:11:32:22 (on wlan0)
        SSID: WIFI1

Nice. Now I have to ensure that, for example, "00:18:46:73:04:52 " and WIRELESS are next each other, meaning that they belong to the same wireless network.
But, how to do that?
With that solved, the script would be finished!

(Last edited by dabyd64 on 19 Aug 2013, 00:06)

Hi, dabyd64,

I have tested it on backfire, doesn't work.Maybe it works, it is that I did't configure it right.

And updated to AA, first repeat my another router, make sure everything works right.

Then win scp the scripts,reboot.

And turned off my router that was repeated, and it didn't connect to the next one on the white list.

Maybe my there's sth wrong with my configs?

1. why there is no rc.common in  /etc/wifiMgr.sh?
2. if there should be a space after c in "net=$(ping google.es -c5 |grep "time=")"? changed to "net=$(ping yahoo.com -c 5 |grep "time=") and another same line too.

Can you put an example of PSK2 encryption? all I can see is:
1 wep
2 wep with shared key
3 wpa-psk
4 wpa2-psk
5 wpa-psk/wpa2-psk mixed mode

what for these 5 accordingly? Sorry, maybe too basic.

Can you show me your major configs?

Thanks so much.

(Last edited by eeff11 on 4 Sep 2013, 07:53)