Posted: Tue Jul 03, 2012 23:36 Post subject: Changing client mode network from script - WZR-HP-G300NH2
Hi All,
I've been trying to write some scripts to connect to different wireless networks in client mode. I'm getting some unexpected results so hope someone might point me in the right direction.
The script brings down ath0, changes the ssid, momentarily flicks over to AP mode with disabled net mode then back to Client mode with Mixed net mode, brings back up ath0, then reset udhcp client to connect to a different network.
It's based originally on the autoap_fonera scripts and i've had some good results but one big issue keeps cropping up.
After running the script the Status->Wireless GUI shows the SSID has changed. During the script running, i can see the GUI showing the radio go off and back on, the wan ip being released etc, mode changing to AP and then back to client OK.
However when the script finishes the router remains connected to the old network. Under wireless nodes - Access point it shows the mac address of the AP on the old network, and the WAN IP renews to the old IP. The SSID shown is the new network, but it is actually still connected to the old network, and i can still connect to clients on the old network.
Please see script below:
Code:
#Bring down Ath0, set wireless to ap with no ssid and disabled
ifconfig ath0 down
ifconfig ath0 0.0.0.0
nvram set ath0_ssid=""
nvram set wan_get_dns=0.0.0.0
nvram set wan_gateway=0.0.0.0
nvram set wan_ipaddr=0.0.0.0
nvram set wan_lease=0
iwconfig ath0 mode managed
iwconfig ath0 key off
nvram set ath0_mode=ap
nvram set ath0_net_mode=disabled
nvram set wan_proto=disabled
nvram set wanup=0
sleep 3
#Set new ssid, client mode and mixed mode
iwconfig ath0 key off
iwconfig ath0 essid NEWNETWORK
nvram set ath0_ssid=NEWNETWORK
nvram set ath0_mode=sta
nvram set ath0_net_mode=mixed
sleep 1
#Toggle ath0 up, down, up to be safe (from autoap_fonera)
ifconfig ath0 up
sleep 5
ifconfig ath0 down
sleep 5
ifconfig ath0 up
sleep 3
#Kill udhcpc client to get new ip
kill -USR2 $(cat /tmp/var/run/udhcpc.pid) > /dev/null 2>&1
killall udhcpc > /dev/null 2>&1
udhcpc -i ath0 -p /tmp/var/run/udhcpc.pid -s /tmp/udhcpc > /dev/null 2>&1 &
#Set wan back to dhcp
nvram set wan_proto=dhcp
The only way round this is to run an nvram commit and reboot after the script. After reboot i'm connected to the new network.
I can also do this manually through the GUI and then force the udhcpc restart, then it works perfectly, connected to the new network.
Code:
Start with old network Connected
Change to AP, disabled with no SSID (or SSID as "NONE" for example)
Change to client, mixed mode with NEWNETWORK ssid
Re-enable WAN DHCP
#Then reset udhcp client to renew ip by running
kill -USR2 $(cat /tmp/var/run/udhcpc.pid) > /dev/null 2>&1
killall udhcpc > /dev/null 2>&1
udhcpc -i ath0 -p /tmp/var/run/udhcpc.pid -s /tmp/udhcpc > /dev/null 2>&1 &
Seems to make no difference whether the wan is set back to dhcp before or after the udhcpc reset.
Hopefully i've missed something very obvious!!! Any help greatly appreciated. I'm guessing i need something to totally reset ath0 but not sure what i'm missing.
PS - just in case - I'm using a WZR-HP-G300NH2 with brainslayer 17461 - i tried newer builds but had big issues with client mode - 17461 was the only one i could get to connect to any network. Anyone have success connecting the G300NH as a client on any newer builds?
Modified the script as follows to include stop and re-start service of both wan and lan. With this included there is no need for the udchpc commands or to bring ath0 back up - the startservice does all this. I had to include the lan as stop/start wan only doesn't work and i still get my old problem of connected to the old network.
Code:
#Bring down Ath0, set wireless to ap with no ssid and disabled
echo "Disabling Ath0"
ifconfig ath0 down
ifconfig ath0 0.0.0.0
nvram set wan_get_dns=0.0.0.0
nvram set wan_gateway=0.0.0.0
nvram set wan_ipaddr=0.0.0.0
nvram set wan_lease=0
iwconfig ath0 mode managed
#Set new connection settings
echo "Writing new settings"
iwconfig ath0 key off
nvram set ath0_ssid=NEWNETWORK
iwconfig ath0 essid NEWNETWORK
nvram set ath0_mode=sta
nvram set ath0_net_mode=mixed
nvram set wan_proto=dhcp
sleep 5
echo "Starting lan service"
#Restart lan and wan service to clear old connection
sleep 5
stopservice wan; stopservice lan; wait 3; startservice lan; startservice wan
But...! This works perfectly the first time i run it, e.g. router boots, auto connects to OLDNETWORK (from nvram), all ok.
Then I run the script, connects to NEWNETWORK ok, gets new ip, verified connected to correct network, all fine.
However if i try and run a version of the script again with OLDNETWORK as the ssid, it runs ok, GUI shows everything fine, but about 15-20 seconds afterwards the router either hangs (usual outcome) or reboots (rarely).
Any ideas? I think i'm getting there but not quite there yet!!!
#Reset wan info
nvram set wan_get_dns=0.0.0.0
nvram set wan_gateway=0.0.0.0
nvram set wan_ipaddr=0.0.0.0
#Disable ath0 and commit
nvram set ath0_net_mode=disabled
nvram commit
sleep 2
#Set new connection settings
echo "Writing new settings"
iwconfig ath0 key off
nvram set ath0_ssid=NEWNETWORK
nvram set at0_scanlist=
nvram set ath0_mode=sta
nvram set ath0_net_mode=mixed
nvram set wan_proto=dhcp
#Reset ath0
ifconfig ath0 down
sleep 5
ifconfig ath0 up
#Reenable ath0 wireless mode and commit
nvram set ath0_net_mode=enabled
nvram commit
#Restart wland
killall wland
sleep 1
wland
sleep 1
#Start wan and lan (as they're already running it seems to restart them)
startservice wan
startservice lan
sleep 2
#Restart udhcp client to get new ip
kill -USR2 $(cat /tmp/var/run/udhcpc.pid) > /dev/null 2>&1
killall udhcpc > /dev/null 2>&1
udhcpc -i ath0 -p /tmp/var/run/udhcpc.pid -s /tmp/udhcpc > /dev/null 2>&1 &
Tested it about 20 times flipping between two networks, works great
The startservice lan causes one lan ping to drop (about 1-2 sec of lan downtime) but otherwise router stays up
Thank you very much for this!!
This is 50% what I was looking for. I will try to combine it with another script that test the connection status and then we have a working failover mechanism!
Thank you very much for this!!
This is 50% what I was looking for. I will try to combine it with another script that test the connection status and then we have a working failover mechanism!
Thanks
Hey, I was wondering did you ever finish your script for failover? I'm trying to do something similar. I have access to public wifi but its slow so I normally use my unlimited data hotspot when I'm home. I was hoping to come up with a way to have the router switch to my hotspot when its available and back to the public when I'm away.