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.

Hi dabyd64,

Could you please help tell me the reason you created the new scan0 interface? Why didn't you use wlan0 to scan?

BTW, I think that the file /etc/init.d/wifiMgr missed " /etc/rc.common" in the first line.

Thanks and regards,
Averell

(Last edited by Averell on 3 Sep 2013, 03:17)

Averell wrote:

Hi dabyd64,

Could you please help tell me the reason you created the new scan0 interface? Why didn't you use wlan0 to scan?

BTW, I think that the file /etc/init.d/wifiMgr missed " /etc/rc.common" in the first line.

Thanks and regards,
Averell


Hi,Averell,

I added #!/bin/sh /etc/rc.common as you informed.

yes, now the script seems working, but errors as follows:

root@OpenWrt:~# /etc/init.d/wifiMgr restart
root@OpenWrt:~# Current MAC : 11:22:33:44:55:66 (unknown)
ERROR: Can't change MAC : interface up or not permission :Device or resource busy
ping: bad address 'yahoo.com'
killall : hostpad: no process killed
command failed: Device or resource busy (-16)
command failed: not found
Successfully initialized wpa_supplicant
ping: bad address 'yahoo.com'
sleep: invalid number 'net='
command failed: Device or resource busy (-16)
command failed: not found
Successfully initialized wpa_supplicant
ping: bad address 'yahoo.com'
sleep: invalid number 'net='
command failed: Device or resource busy (-16)
command failed: not found
Successfully initialized wpa_supplicant
ping: bad address 'yahoo.com'
sleep: invalid number 'net='

And on the webgui, it shows it tries to connet to the white list, but failed at each one?

Where possibly should I check?

eeff11 wrote:

Hi, dabyd64,

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.

Check this: http://wiki.openwrt.org/doc/uci/wireless#configuring.encryption
Look at "WPA Modes".

Maybe you copied something wrong. If you copied / edited using windows, usually it won't work.
I uploaded the files here: https://dl.dropboxusercontent.com/u/23958662/wifiMgr.zip
Just follow the Readme.txt

My wireless config:

config wifi-device 'radio0'
        option type 'mac80211'
        option path 'pci0000:00/0000:00:00.0'
        list ht_capab 'SHORT-GI-40'
        list ht_capab 'TX-STBC'
        list ht_capab 'RX-STBC1'
        list ht_capab 'DSSS_CCK-40'
        option country 'BO'
        option txpower '17'
        option hwmode '11ng'
        option htmode 'HT20'
        option channel '2'

config wifi-iface
        option short_preamble '1'
        option device 'radio0'
        option network 'wwan'
        option mode 'sta'
        option encryption 'psk2'
        option key '2sss3sd305s'
        option ssid wifi1''
Averell wrote:

Hi dabyd64,

Could you please help tell me the reason you created the new scan0 interface? Why didn't you use wlan0 to scan?

BTW, I think that the file /etc/init.d/wifiMgr missed " /etc/rc.common" in the first line.

Thanks and regards,
Averell

I found problems when the wlan0 interface was disconected, sometimes it would hang!
Disabling it, creating scan0 and then enabling wlan0 again after scanning worked perfectly in all the tests I made.

About rc.common..well, it's working without it. So I didn't add it tongue
Edit: It was there, but the file I copied in that moment didn't had it. My bad!

eeff11, it looks that you copied something wrong. This errors makes no sense. It just does "sleep 30"

sleep: invalid number 'net='

(Last edited by dabyd64 on 15 Oct 2013, 23:27)

dabyd64 wrote:
Averell wrote:

Hi dabyd64,

Could you please help tell me the reason you created the new scan0 interface? Why didn't you use wlan0 to scan?

BTW, I think that the file /etc/init.d/wifiMgr missed " /etc/rc.common" in the first line.

Thanks and regards,
Averell

I found problems when the wlan0 interface was disconected, sometimes it would hang!
Disabling it, creating scan0 and then enabling wlan0 again after scanning worked perfectly in all the tests I made.

About rc.common..well, it's working without it. So I didn't add it tongue

having /etc/rc.common would help enabling the script. I think you must have had that line once, then you enabled the script, never disable the script after that, and removed that line. Otherwise, how can you have that wifiMgr started up automatically?
I prefer a standardized init script (putting wifiMgr.sh into /usr/bin/ instead of /etc/:

#!/bin/sh /etc/rc.common

START=99
APP=wifiMgr.sh
PID_FILE=/var/run/$APP.pid

start() {
        start-stop-daemon -S -x $APP -p $PID_FILE -m -b
}

stop() {
        start-stop-daemon -K -n $APP -p $PID_FILE -s TERM
        rm -rf $PID_FILE
}

(Last edited by Averell on 7 Sep 2013, 08:11)

who knows, maybe you're right! I'll add that line then!

****edit***
It was already there! I may have copied it wrong when I did the post. It exists on the downloadable file...
I edited and changed wifiMgr as you posted. Now it's a lot better! Also edited the locations to /usr/bin
As I said, my knowledge about linux is pretty poor. But not in Windows! (I'm ready for my public execution after saying this lol)

Updated the downloadable zip file smile

(Last edited by dabyd64 on 9 Sep 2013, 14:50)

Some nice updates there. Your feedback is very appreciated smile

(Last edited by dabyd64 on 13 Sep 2013, 01:25)

dabyd64 wrote:

Some nice updates there. Your feedback is very appreciated smile

Yes, thanks so much, dabyd64,

with your extensive help, it now WORKS as each boot connnecting to the one as in the wlist, #1, #2, etc.

The remaining problem,is the bssid. If using the luci web to MANUALLY repeat a essid,"join the network" actually, cat etc/config/wireless, there is a
"option bssid xx:xx:xx:xx:xx:xx", what are really the difficulties if define the variant "bssid", and say put the variant "bssid" in the script like " uci set wireless.@wifi-iface[0].bssid="$bssid".

This bssid is that I am repeating the free city wifi, they got several same essids, but different bssid, in UBNT airos, you can "LOCK" a bssid when repeating as a client in it's web config interface.But really no idea in Openwrt, too many "inside tricks". http://wiki.ubnt.com/images/b/b2/Airos_link_station_wds.png

Other issue is how to use the AA 12.09 macchanger.ipk, seached a lot, seems no one ever got success,either before the wlan0 is up or after.Since the free city wifi has a 12 hour connection limit per mac, so I need a random wwan mac each boot of the router.AAP v3 can do it perfectly.

One last serious problem is that: does the script writes to the flash each time it tried a new ap on the wlist,write to the etc/config/wireless, doesn't it? This is SERIOUS, 'cause the NOR flash has a limited life, though claimed to be 100,000 times rewriteable, SERIOULY DOUBT THAT. NO STRANGELY ENOUGH,SAMSUNG'S NAND FLASH develop algorithms for bad blocks.If some block is damaged for frequent write, the whole router is dead. The AAP seems to do this by writes in the RAM, /tmp....

When you have time,pls check here , 7,8,9,10 floor,https://forum.openwrt.org/viewtopic.php?pid=94025#p94025
Each script shall minimize the write to the flash as little as possible.
Lux has did a goog job, No. 4 of https://forum.openwrt.org/viewtopic.php?pid=119392#p119392

Except for that, this script is REALLY TWO THUMBS up, can replace AAP v3, as that only supports madwifi, not ath9k!

Thanks.

(Last edited by eeff11 on 4 Oct 2013, 09:53)

Hi,
The script executes UCI commands. It doesn't manage directly any file but his "config".
So, whatever it does I guess that yes, it writes to Flash.
But remember that each cell can be erased >100K times. The filesystem prevents you to always modify the same sector, so when you make any change, the data in the current sector is not modified, but copied to another different sector.
In most cases, each sector is sized in 64KBytes, having only ~300KB free, means 5 sectors. That is 500K writes!
You have to modify the file 500 times a day for 3 years! By the way, I never heard anyone having killed it's router's  flash!
NAND has millions of sectors, it's normal that some fail. But serial flash has only 4, 8, 16MB, they don't have replacement sectors and they are not designed to allow fails, so probably you will have to replace the flash chip .

I've just updated the script.
Inside the config file, set the variable randMac to "1" to make the script to set a random mac on each boot. smile

Thanks to google and a clever guy I found a nice way to generate it without needing to install any openwrt package:

 mac=(dd if=/dev/urandom bs=1024 count=1 2>/dev/null|md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/')

I still have to find how to know when a BSSID and SSID are from the same network, and how to detect a network that has the same SSID but different BSSID.

(Last edited by dabyd64 on 8 Oct 2013, 00:10)

dabyd64 wrote:

Hi,
The script executes UCI commands. It doesn't manage directly any file but his "config".
So, whatever it does I guess that yes, it writes to Flash.
But remember that each cell can be erased >100K times. The filesystem prevents you to always modify the same sector, so when you make any change, the data in the current sector is not modified, but copied to another different sector.
In most cases, each sector is sized in 64KBytes, having only ~300KB free, means 5 sectors. That is 500K writes!
You have to modify the file 500 times a day for 3 years! By the way, I never heard anyone having killed it's router's  flash!
NAND has millions of sectors, it's normal that some fail. But serial flash has only 4, 8, 16MB, they don't have replacement sectors and they are not designed to allow fails, so probably you will have to replace the flash chip .

I've just updated the script.
Inside the config file, set the variable randMac to "1" to make the script to set a random mac on each boot. smile

Thanks to google and a clever guy I found a nice way to generate it without needing to install any openwrt package:

 mac=(dd if=/dev/urandom bs=1024 count=1 2>/dev/null|md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/')

I still have to find how to know when a BSSID and SSID are from the same network, and how to detect a network that has the same SSID but different BSSID.

You're clever too and most important, you're resourceful and so helpful.

I will report after test.

I found that AAP v3 use bssid to identify a wifi, maybe that helps you a little.

Hi, dabyd64,

the random mac worked when changed the script and rebooted, it still worked,two random MacS.

But today, when I powered on the router, the mac still is the orginal one, random mac didn't work. When I rebooted, it remains the same, and I rebooted for the third time, the random Mac works. Strange.

I am still testing.

And I notice there is some change in the main loop NetChange, but basically the change is almost doing the same thing as the last version.Why the change? The connection seems to drop more frequently than the last version, maybe today's radio disturbance around me is worse, irrelevant to the script.

tested 2 days, some time boot, a random Mac, sometime, it's the orginal   Mac, flashed all again with wiping all settings, set up from Zero, same strange thing, sometime random, sometime the original Mac, strange, looked into the script, seems correct, really no idea.

another issue I asked help in another post in general discussion. AA 12.09 locked to US regdomain, which makes no sense since the stock firmware from Tp-Link can make use of channel 12,13.AA 12.09 makes the "openwrt" not so open.Really have to compile again with "option force...."?

Some macaddresses give issues, set the first octet of the mac address fixed to a value.

(Last edited by FriedZombie on 11 Oct 2013, 12:26)

FriedZombie wrote:

Some macaddresses give issues, set the first octet of the mac address fixed to a value.

hi, except that set the first octet fixed, better solution? The first octet value fixed makes the random mac function less meaningful.

Any mac with bit 0 (broadcast) unset and bit 1 (locally administered) set is a valid mac.
So performing binary ((n & 0x7F) | 0x02) on the first byte will always yield a good usable address.

eeff11 wrote:

[...]Really have to compile again with "option force...."?

Yes. I don't understand that, it's something like "you must compile your own to enable illegal channels on US"

FriedZombie wrote:

Some macaddresses give issues, set the first octet of the mac address fixed to a value.

jow wrote:

Any mac with bit 0 (broadcast) unset and bit 1 (locally administered) set is a valid mac.
So performing binary ((n & 0x7F) | 0x02) on the first byte will always yield a good usable address.

So, better to force what bytes to what value?
Some time ago I had problems with router when the first byte was not 00, ex. 93:22:15:ec:5d:47
But none if it was 00:xx:xx:xx:xx:xx

Forcing the mac to be xx:xx:xx:xx:xx:02 would work always?

I have updated the script, all the generated MAC addresses will be 00:xx:xx:xx:xx:02. Let me know if it works now wink

(Last edited by dabyd64 on 15 Oct 2013, 23:22)

It is interesting to add to the script to connect to open access points as well as an automatic search through a password reaver

Wondering if this could work on dd-wrt? I have autoap working on my dd however only accepts open connections.

Hi dabyd64,

I try to install your script but i got this error:
root@OpenWrt:~# /etc/init.d/wifiMgr restart
no wifiMgr.sh found; none killed
root@OpenWrt:~# cd /usr/bin/
root@OpenWrt:/usr/bin# ls
[               cut             ivstools        passwd          test
[[              dbclient        jshn            pgrep           time
aircrack-ng     dirname         killall         printf          top
airdecap-ng     dropbearkey     kstats          readlink        tr
airdecloak-ng   du              less            reaver          traceroute
arping          env             logger          reset           uniq
awk             expr            lua             scp             uptime
basename        find            luci-bwc        seq             wash
buddy-ng        free            makeivs-ng      sort            wc
bunzip2         haserl          md5sum          ssh             wget
bzcat           head            mkfifo          strings         which
clear           hexdump         nc              tail            wifiMgr.sh
cmp             hostid          nslookup        tee             xargs
crontab         id              packetforge-ng  telnet          yes
root@OpenWrt:/usr/bin#

In the wifiMgr file use the default option.
APP=/usr/bin/wifiMgr.sh

Could you help me?

Thanks

Did you set the correct permissions with chmod?

dabyd64 wrote:
jow wrote:

Any mac with bit 0 (broadcast) unset and bit 1 (locally administered) set is a valid mac.
So performing binary ((n & 0x7F) | 0x02) on the first byte will always yield a good usable address.

Forcing the mac to be xx:xx:xx:xx:xx:02 would work always?

I have updated the script, all the generated MAC addresses will be 00:xx:xx:xx:xx:02.

Actually should be first octet, so force 02:xx:xx:xx:xx:xx. I think you are having problems if you set the broadcast bit (e.g. 97:xx:xx:xx:xx:xx would be a problem because it will be a multicast/broadcast address)

https://en.wikipedia.org/wiki/MAC_address#Address_details

Well, almost all computers/devices start with 00:xx:xx:xx:xx:xx:xx so its a bit confusing...

dabyd64 wrote:

Did you set the correct permissions with chmod?


Hi Dadyd64,

Yes. I've set.

I've installed another router and i have same error message.

Could you https://dl.dropboxusercontent.com/u/23958662/wifiMgr.zip download the file and try to set again with your router?

My openwrt version: Attitude Adjustment 12.09

Thx

Something has gone wrong, that is not the latest version of the script???
I'll check it right now.

*EDIT*

Yes, at some point I uploaded a wrong file, it was dated of 9-9-2013 while the latest and final version was done on 16-9-2013.
But part of the files did belong to the latest version, so I guess that I mixed the files somehow.
I have fixed it, the link is still the same.
Please delete all the related files before trying the latest version
Sorry for the mistake! sad

(Last edited by dabyd64 on 3 Jun 2014, 21:33)