1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

tomato as wireless client - failover AP

Discussion in 'Tomato Firmware' started by rs232, Feb 22, 2011.

  1. rs232

    rs232 Network Guru Member

    Hi all, I have a question:

    I have one my my tomato routers set as wireless client, what are the shell command line to control the wireless settings?

    As I have 2 AP in range I'd like to:

    1) poll if AP 1 is available, if so connect using a specified encryption, key and Ip address

    2) is not connect to AP 2 with different encryption key and IP

    Any help on how to get started with this?

    The idea is to have a script running every e.g. hour to provide a sort of resiliency in case AP 1 goes down,

    Thanks!
    rs232
     
  2. RonWessels

    RonWessels Network Guru Member

    You do realize that, if you ever fall back to AP#2, your client will disconnect briefly every hour until AP#1 comes back up? With only one radio, you cannot attempt to connect to AP#1 (polling it) while still connected to AP#2.
     
  3. rs232

    rs232 Network Guru Member

    Not sure I've epress myself properly :)
    I'm looking to connect to one AP at the time. Of course when the switch over happens connectivity is lost, but I'm ok with it as long as it doesn't happen too often.
    Using the web interface tools/Wireless Survey I can gather information on other networks/AP without disconnect. I would just need the basic commands to work on, the rest should be rather straight forward.
     
  4. TT76

    TT76 Networkin' Nut Member

    RonWessels is right, you have to put up with that.
    I haven't tested it, so I don't know if it will work. I have an idea that you can turn off AP2 after AP1 recovers and then turn on AP2 after the wireless client switches to AP1. So that is script 2
    scrip 1
    Code:
    while :
    do
    i1=0
    i2=0
    eval $(ping AP1addr -c3 -W1 | awk '(NR==2||NR==3||NR==4)&&substr($0,1,1)=="6"{print "i1=1";NR=8}')
    if [ $i1-ne1 ];
    then
    nvram set wl0_ssid=AP2ssid
    nvram set wl0_wpa_psk=client2key
    nvram set wl0_ipaddr=client2addr
    kill -1 1
    sleep 60
    eval $(ping AP2addr -c3 -W1 | awk '(NR==2||NR==3||NR==4)&&substr($0,1,1)=="6"{print "i1=1";NR=8}')
    if [ $i2-eq1 ];
    then
    sleep 3600
    fi
    nvram set wl0_ssid=AP1ssid
    nvram set wl0_wpa_psk=client1key
    nvram set wan_ipaddr=client1addr
    kill -1 1
    sleep 60
    fi
    sleep 10
    done
    script 2
    Code:
    while :
    do
    i1=0
    i2=0
    eval $(ping AP1addr -c3 -W1 | awk '(NR==2||NR==3||NR==4)&&substr($0,1,1)=="6"{print "i1=1";NR=8}')
    eval $(ping AP2addr -c3 -W1 | awk '(NR==2||NR==3||NR==4)&&substr($0,1,1)=="6"{print "i1=1";NR=8}')
    if [ $i1-ne1-a$i2-ne1 ];
    then
    nvram set wl0_ssid=AP2ssid
    nvram set wl0_wpa_psk=client2key
    nvram set wl0_ipaddr=client2addr
    kill -1 1
    sleep 60
    eval $(ping AP2addr -c3 -W1 | awk '(NR==2||NR==3||NR==4)&&substr($0,1,1)=="6"{print "i1=1";NR=8}')
    fi
    if [ $i1-ne1-a$i2-ne1 ];
    nvram set wl0_ssid=AP1ssid
    nvram set wl0_wpa_psk=client1key
    nvram set wan_ipaddr=client1addr
    kill -1 1
    sleep 60
    fi
    sleep 30
    done
     
  5. RonWessels

    RonWessels Network Guru Member

    Are you sure? I believe a Wireless Survey causes a temporary disconnect, followed by an automatic reconnect afterward. This is the interruption of which I spoke.
     
  6. rs232

    rs232 Network Guru Member

    You may well be right, but if that's that case that disconnection is not relevant to me as I'm not able to notice when it happens.
     
  7. rs232

    rs232 Network Guru Member

    TT76: thanks!
    I haven't tested your scripts yet but they look to be very close to what I'm after.
    I think the right approach for me here is to create one script per AP available. Even using the router button to switch between them would be a great starting point to me, adding the ping test only later on...

    What I would need to do switching between access points is:

    1) disconnect from current AP

    2) Set new:
    - encryption (one is WEP the other is WPA2)
    - key
    - IP address (one AP is DHCP the other is t static)
    - DNS servers
    - subnet + gateway

    3) connect



    I guess what I'm asking is:

    1a) what command should I type to disconnect

    2a) what nvram keys should I set according to point 2 above

    3a) what command should I type to connect


    About point 2a I guess a nvram dump of the two access point config and the diff utility is all I need, still don't know how to achieve 1a and 3a (unless it's a trivial radio off + radio on)

    Thanks a lot very very helpful!
    :)
    rs232
     
  8. TT76

    TT76 Networkin' Nut Member

    The command "kill -1 1" is used to inform init process to reread nvram settings ,that is to disconnect and reconnect network according new nvram settings including DNS DHCP. There are a lot of things needed to do before radio on, so the command is only way, and I got it from tomato source code.:biggrin:
     
  9. rs232

    rs232 Network Guru Member

    Thanks, that's great!
    I'm going throught the diff of the nvram dump.
    Any idea why there are lot of duplicated keys? example:

    Code:
    wl0_mode=sta
    wl0_nbw=0
    wl0_net_mode=g-only
    wl0_nmcsidx=-2
    wl0_nmode=0
    wl0_radio=1
    Code:
    wl_mode=sta
    wl_nbw=0
    wl_net_mode=g-only
    wl_nmcsidx=-2
    wl_nmode=0
    wl_radio=1
     
  10. rs232

    rs232 Network Guru Member

    It seems like that I need to set both keys wl and wl0

    Any help/comment?
    Thanks!
     
  11. TT76

    TT76 Networkin' Nut Member

    you only need to set wl0.
     
  12. rs232

    rs232 Network Guru Member

    great! many thanks!

    :)
     
  13. rs232

    rs232 Network Guru Member

    DONE!

    Thanks to everybody for the help :)

    I'm using this in the administration/buttons-led/custom-script config

    Code:
    WL0=`cat /tmp/switch.var`
    if [ -z "$WL0" ]
    then
    WL0=1
    fi
    
    WLCOUNT=`nvram get wlswitch`
    
    if [ $WL0 -eq 1 ]
    then
    echo 1
    radio off
    nvram set security_mode=wep
    nvram set wan_gateway=192.168.2.1
    nvram set wan_get_domain=AP1
    nvram set wan_ipaddr=192.168.2.222
    nvram set wan_netmask=255.255.255.0
    nvram set wan_proto=static
    nvram set wl0_akm=
    nvram set wl0_key1=11111111111111111111111111
    nvram set wl0_ssid=AP1
    nvram set wl0_wep=enabled
    kill -1 1
    radio on
    echo 2 > /tmp/switch.var
    
    else
    echo 2
    radio off
    nvram set security_mode2=wpa2_personal
    nvram set security_mode=psk2
    #nvram set wan_gateway=0.0.0.0
    nvram set wan_get_domain=
    #nvram set wan_ipaddr=0.0.0.0
    #nvram set wan_netmask=0.0.0.0
    nvram set wan_proto=dhcp
    nvram set wl0_akm=psk2
    nvram set wl0_crypto=aes
    nvram set wl0_wep=disabled
    nvram set wl0_key1=
    nvram set wl0_wpa_psk=22222222222222222222222222222
    nvram set wl0_ssid=AP2
    kill -1 1
    radio on
    echo 1 > /tmp/switch.var
    fi
    WLCOUNT=$(($WLCOUNT+1))
    nvram set wlswitch=$WLCOUNT
    
    every time I press the button it automatically switches from WEP network with static IP to WPA network with DHCP.
    Ideally the first case defined in the script should be your "secondary" network where the script's second case could be e.g. your primary network (the one you already defined in the basic settings and loads by default after a reboot). This is because the first time you press the button it will always run case 1.

    alternatively/additionally you could script this to run in case of ping failure or what so ever, if this is the case you me interested into running

    Code:
    nvram get wlswitch
    to see how many times the script switched connection.

    Cheers
    rs232 :)
     
  14. rs232

    rs232 Network Guru Member

    Actually just a little problem saving the script into button/led

    I get a:
    Code:
    The field "sesx_script" is invalid. Please report this problem.
    Is this a tomato bug?

    Running the script from the shell it works fine.
     
  15. rs232

    rs232 Network Guru Member

    Hum... removing these line solved the problem:

    WLCOUNT=`nvram get wlswitch`

    WLCOUNT=$(($WLCOUNT+1))
    nvram set wlswitch=$WLCOUNT

    Not sure why though...

    any ways the final script now look like this:

    Code:
    WL0=`cat /tmp/switch.var`
    if [ -z "$WL0" ]
    then
    WL0=1
    fi
    
    if [ $WL0 -eq 1 ]
    then
    echo 1
    radio off
    nvram set security_mode=wep
    nvram set wan_gateway=192.168.2.1
    nvram set wan_get_domain=AP1
    nvram set wan_ipaddr=192.168.2.222
    nvram set wan_netmask=255.255.255.0
    nvram set wan_proto=static
    nvram set wl0_akm=
    nvram set wl0_key1=11111111111111111111111111
    nvram set wl0_ssid=AP1
    nvram set wl0_wep=enabled
    kill -1 1
    radio on
    echo 2 > /tmp/switch.var
    
    else
    echo 2
    radio off
    nvram set security_mode2=wpa2_personal
    nvram set security_mode=psk2
    #nvram set wan_gateway=0.0.0.0
    nvram set wan_get_domain=
    #nvram set wan_ipaddr=0.0.0.0
    #nvram set wan_netmask=0.0.0.0
    nvram set wan_proto=dhcp
    nvram set wl0_akm=psk2
    nvram set wl0_crypto=aes
    nvram set wl0_wep=disabled
    nvram set wl0_key1=
    nvram set wl0_wpa_psk=22222222222222222222222222222
    nvram set wl0_ssid=AP2
    kill -1 1
    radio on
    echo 1 > /tmp/switch.var
    fi
     
  16. rs232

    rs232 Network Guru Member

    I have a final question on this topic:
    When tomato boots up, where is it reading the configuration from?
    I've somehow took for granted this was the NVRAM, but running a test on 3 AP, one defined on the GUI (and saved using the save button) and other 2 set using the script discussed above; no matter what it will always load AP1 (GUi defined) after a tomato reboot.

    So I assume the save button saves the config in e.g. a file and tomato reads this file when booting up setting (not reading) the NVRAM instead. Is this correct?

    Thanks!
    rs232
     

Share This Page