OpenWrt Forum Archive

Topic: [howto] Failover Capability script between two wan (DSL and 3G).

The content of this topic has been archived between 20 Apr 2018 and 22 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Ok, thank you! Where to put in the last row? At first in the script?
Next problem: isn't only USB0, I've got a dongle USB0, one USB3 and one ACM0. Want to use them free of thinking about that.

FriedZombie wrote:

argh should pay more attention hmm

wink

Thank you for your help! At first:

also you can hold the script till the tty device is availlable.

 while [ ! -e "/dev/ttyUSB0" ]; do sleep 5; done

this snippet will wait till the file exists
or you could write something to wait for the network devices.

I don't think that this will work: I think the problem is that the argument, written from within /etc/rc.local, comes too late to initialize the Stick:

echo "12d1 1003" > /sys/bus/usb-serial/drivers/option1/new_id

And without that there are no USB-devices.

And now the second: The script does not work for me. I guess it's because I have an extra (virtual) eth1 device for accessing my VDSL-Modem. When running the script it trys got get the gateway from this (wrong) device. There's no gw, so it starts immediately wan2 over 3g. Here are some informations:

~# cat /etc/config/network
config 'interface' 'loopback'
    option 'ifname' 'lo'
    option 'proto' 'static'
    option 'ipaddr' '127.0.0.1'
    option 'netmask' '255.0.0.0'

config 'interface' 'lan'
    option 'type' 'bridge'
    option 'proto' 'static'
    option 'netmask' '255.255.255.0'
    option 'broadcast' '192.168.7.255'
    option 'ipaddr' '192.168.1.1'
    option '_orig_ifname' 'eth0 wlan0 wlan1'
    option '_orig_bridge' 'true'
    option 'ifname' 'eth0'

config 'interface' 'wan'
    option 'ifname' 'eth1'
    option '_orig_ifname' 'eth1'
    option '_orig_bridge' 'false'
    option 'proto' 'pppoe'
    option 'username' 'xxxx@xxxx.de'
    option 'password' 'xxx'

config interface 'ppp0'
    option ifname 'ppp0'
    option device '/dev/ttyUSB0' # fuer Huawei E172
    option service 'umts'
    option proto '3g'
    option auto '0'

config 'interface' 'guest'
    option 'type' 'bridge'
    option 'proto' 'static'
    option 'ipaddr' '192.168.2.1'
    option 'netmask' '255.255.255.0'
    option '_orig_ifname' 'wlan0-1 wlan1-1'
    option '_orig_bridge' 'true'

config 'switch'
    option 'name' 'switch0'
    option 'reset' '1'
    option 'enable_vlan' '1'

config 'switch_vlan'
    option 'device' 'switch0'
    option 'vlan' '1'
    option 'ports' '0 1 2 3 4'

# config 'alias' 'modem'
config 'interface' 'modem'
    option 'proto' 'static'
    option 'ifname' 'eth1'
    option 'ipaddr' '192.168.150.5'
    option 'netmask' '255.255.255.248'
~# ifconfig
br-guest  Link encap:Ethernet  HWaddr xxx
          inet addr:192.168.2.1  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: xxx/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:810 (810.0 B)

br-lan    Link encap:Ethernet  HWaddr xxx
          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: xxx/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6271 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6065 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:383052 (374.0 KiB)  TX bytes:988493 (965.3 KiB)

eth0      Link encap:Ethernet  HWaddr xxx
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6276 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6065 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:471164 (460.1 KiB)  TX bytes:988493 (965.3 KiB)
          Interrupt:4 

eth1      Link encap:Ethernet  HWaddr xxx
          inet addr:192.168.150.5  Bcast:192.168.150.7  Mask:255.255.255.248
          inet6 addr: xxx/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1349 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1377 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:177988 (173.8 KiB)  TX bytes:115128 (112.4 KiB)
          Interrupt:5 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:120 errors:0 dropped:0 overruns:0 frame:0
          TX packets:120 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:10474 (10.2 KiB)  TX bytes:10474 (10.2 KiB)

pppoe-wan Link encap:Point-to-Point Protocol  
          inet addr:7.2.3.2  P-t-P:2.0.9.1  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1492  Metric:1
          RX packets:829 errors:0 dropped:0 overruns:0 frame:0
          TX packets:856 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3 
          RX bytes:128464 (125.4 KiB)  TX bytes:80206 (78.3 KiB)
[...]
~# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0           2.0.9.1           0.0.0.0         UG    0      0        0 pppoe-wan
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 br-guest
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 br-lan
192.168.150.0   0.0.0.0         255.255.255.248 U     0      0        0 eth1
2.0.9.1    0.0.0.0         255.255.255.255 UH    0      0        0 pppoe-wan

And the output from sh -x /usr/bin/failover:

~# sh -x /usr/bin/failover.sh
+ WAN1=wan
+ WAN2=ppp0
+ INTERVAL=2
+ PACKETS=1
+ date
+ logger=logger -p daemon.info Tue Sep 17 18:53:01 CEST 2013: /usr/bin/failover.sh 
+ logger -p daemon.info Tue Sep 17 18:53:01 CEST 2013: /usr/bin/failover.sh started
+ awk -F = {print $2}
+ uci show network.wan.gateway
+ DEFAULT_GATEWAY=interface
eth1
eth1
false
pppoe
xxxx@xxxx.de
xxxx
+ awk -F = {print $2}
+ uci show network.wan.ifname
+ WAN1_IF=eth1
+ awk -F = {print $2}
+ uci show network.ppp0.ifname
+ WAN2_IF=ppp0
+ ifdown ppp0
+ ifup wan
+ USINGWAN=1
+ GATEWAY=
+ WAN1_IP=
+ COUNTER=0
+ [  ==  ]
+ logger -p daemon.info Tue Sep 17 18:53:01 CEST 2013: /usr/bin/failover.sh Waiting for interface wan IP
+ + awkawk {if($1=="inet")print $2} -F
 : {print $2}
+ ifconfig eth1
+ WAN1_IP=192.168.150.5
+ grep+  G[ \t]awk
 {print $2}
+ route -n
+ GATEWAY=
+ let COUNTER=COUNTER+1
+ [ 1 -gt 9 ]
+ sleep 2
+ [ 192.168.150.5 ==  ]
+ [  ==  ]
+ logger -p daemon.info Tue Sep 17 18:53:01 CEST 2013: /usr/bin/failover.sh Waiting for interface wan IP
+ awk -F : {print $2}
+ awk {if($1=="inet")print $2}
+ ifconfig eth1
+ WAN1_IP=192.168.150.5
+ awk {print $2}
+ grep G[ \t]
+ route -n
+ GATEWAY=
+ let COUNTER=COUNTER+1
+ [ 2 -gt 9 ]
+ sleep 2
+ [ 192.168.150.5 ==  ]
+ [  ==  ]
+ logger -p daemon.info Tue Sep 17 18:53:01 CEST 2013: /usr/bin/failover.sh Waiting for interface wan IP
+ + + ifconfigawkawk eth1 {if($1=="inet")print $2} -F

 : {print $2}
+ WAN1_IP=192.168.150.5
+ + grepawk G[ \t] {print $2}

+ route -n
+ GATEWAY=
+ let COUNTER=COUNTER+1
+ [ 3 -gt 9 ]
+ sleep 2
+ [ 192.168.150.5 ==  ]
+ [  ==  ]
+ logger -p daemon.info Tue Sep 17 18:53:01 CEST 2013: /usr/bin/failover.sh Waiting for interface wan IP
+ awk {if($1=="inet")print $2}
+ ifconfig eth1
+ awk -F : {print $2}
+ WAN1_IP=192.168.150.5
+ + grepawk G[ \t] {print $2}

+ route -n
+ GATEWAY=
+ let COUNTER=COUNTER+1
+ [ 4 -gt 9 ]
+ sleep 2
+ [ 192.168.150.5 ==  ]
+ [  ==  ]
+ logger -p daemon.info Tue Sep 17 18:53:01 CEST 2013: /usr/bin/failover.sh Waiting for interface wan IP
+ + awkawk {if($1=="inet")print $2} -F
 : {print $2}
+ ifconfig eth1
+ WAN1_IP=192.168.150.5
+ awk {print $2}
+ grep G[ \t]
+ route -n
+ GATEWAY=2.0.9.1
+ let COUNTER=COUNTER+1
+ [ 5 -gt 9 ]
+ sleep 2
+ [ 192.168.150.5 ==  ]
+ [ 2.0.9.1 ==  ]
+ sleep 2
+ + + grepawkawk P-t-P: -F
 : {print $2}
+ ifconfig
 {print $1}
+ WAN2_IP=3.3.7.3
+ awk -F : {print $3}
+ grep P-t-P:
+ ifconfig
+ awk {print $1}
+ WAN2_GW=2.0.9.1
+ [ interface
eth1
eth1
false
pppoe
xxxx@xxxx.de
xxxx ==  ]
+ logger -p daemon.info Tue Sep 17 18:53:01 CEST 2013: /usr/bin/failover.sh GATEWAY: 2.0.9.1, DEFAULT_GATEWAY: interface
eth1
eth1
false
pppoe
xxxx@xxxx.de
xxxx, WAN1_IF: eth1, WAN1_IP: 192.168.150.5, WAN2_IF: ppp0, WAN2_IP: 3.3.7.3
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
+ RET=0
+ [ 0 != 1 ]
+ [ 1 == 1 ]
+ logger -p daemon.info Tue Sep 17 18:53:01 CEST 2013: /usr/bin/failover.sh Changing active WAN interface to ppp0!
+ ifup ppp0
+ USINGWAN=2
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
+ RET=0
+ [ 0 != 1 ]
+ [ 2 == 1 ]
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
+ RET=0
+ [ 0 != 1 ]
+ [ 2 == 1 ]
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
+ RET=0
+ [ 0 != 1 ]
+ [ 2 == 1 ]
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
+ RET=0
+ [ 0 != 1 ]
+ [ 2 == 1 ]
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
+ RET=0
+ [ 0 != 1 ]
+ [ 2 == 1 ]
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
+ RET=0
+ [ 0 != 1 ]
+ [ 2 == 1 ]
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
+ RET=0
+ [ 0 != 1 ]
+ [ 2 == 1 ]
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
+ RET=0
+ [ 0 != 1 ]
+ [ 2 == 1 ]
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
+ RET=0
+ [ 0 != 1 ]
+ [ 2 == 1 ]
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
+ RET=0
+ [ 0 != 1 ]
+ [ 2 == 1 ]
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
+ RET=0
+ [ 0 != 1 ]
+ [ 2 == 1 ]
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
+ RET=0
+ [ 0 != 1 ]
+ [ 2 == 1 ]
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
+ RET=0
+ [ 0 != 1 ]
+ [ 2 == 1 ]
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
+ RET=0
+ [ 0 != 1 ]
+ [ 2 == 1 ]
+ sleep 2
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 2.0.9.1
^X+ RET=0
+ [ 0 != 1 ]
+ [ 2 == 1 ]
+ sleep 2
^C

Please don't take too closer looks at the WAN-IPs as I changed them manually. Perhaps I did mistakes sometimes... wink

naturelle wrote:

I think the problem is that the argument, written from within /etc/rc.local, comes too late to initialize the Stick:

echo "12d1 1003" > /sys/bus/usb-serial/drivers/option1/new_id

And without that there are no USB-devices.

it works! but you need add before this line "exit 0"

it works! but you need add before this line "exit 0"

I didn't contested that it works. But it does NOT work for me if the stick is connected while rebooting. Then the USBx-devices were not generated.
It works only if I put the stick in when the router is ready.

Some news:
I've changed the following:
From

WAN1_IF=`uci show network.$WAN1.ifname | awk -F = '{print $2}'`

to

WAN1_IF=pppoe-wan

and from

DEFAULT_GATEWAY=`uci show network.wan.gateway 2>/dev/null | awk -F = '{print $2}'`

to

DEFAULT_GATEWAY=`route -n | grep $WAN1_IF | grep -v 255.* | awk '{print $2}'`

assuming that it would work after these changes. But it didn't. It maked strange things and changed rapidly from wan1 to wan2.
The problem: my provider blocks pings onto the gateway (IP manually anonymized):(

~$ ping 2.0.9.1
PING 2.0.9.1 (2.0.9.1) 56(84) bytes of data.
From 2.0.9.1 icmp_seq=1 Packet filtered

So, in fact this script seems to not work for me hmm

Very nice work. Thanks for sharing.

I'm in a similiar situation that from time to time my provider's 4G wifi-modem loses its connection.
For that case I connected an old K-3565Z 3G USB stick (different provider, slower but more stable network) to my OpenWrt (bridged) router.

If I need to switch the network (WWAN<->WAN3G), I have to press manually the button on my WR1043ND.
So here for anybody who may be interested in my script:

# cat /etc/hotplug.d/button/buttons

#!/bin/sh

if [ "$BUTTON" = "wps" -a "$ACTION" = "pressed" ]
then
    if [ -e /dev/ttyUSB3 ]
    then
        . /lib/functions/network.sh
        if network_is_up wan3g
        then
            logger "3G network shutdown requested..."
            ifdown wan3g
            # wwan has to be restarted, otherwise the route wouldn't change.
            ifdown wwan
            ifup wwan
        else
            logger "3G network startup requested..."
            ifup wan3g
        fi
    elif [ -e /dev/sda1 ]
    then
        if mount | grep -q "^/dev/sda1 "
        then
            logger "USB drive unmount requested..."
            /etc/init.d/samba stop
            umount /dev/sda1
            echo 0 > /sys/class/leds/tp-link\:green\:qss/brightness
        else
            logger "USB drive mount requested..."
            mount /dev/sda1
            /etc/init.d/samba start
            echo 1 > /sys/class/leds/tp-link\:green\:qss/brightness
        fi
    else
        logger "WPS button pressed..."
    fi
fi

This is kind of dirty (e.g. completely ignoring UCI, no error checking) and suited to my special configuration (if I plug a USB storage, the button has a different meaning).
So any improvements would be very welcome.

Additionally one has to modify /etc/hotplug2.rules as described here:

...
SUBSYSTEM ~~ (^net$|^input$|button$|^usb$|^ieee1394$|^block$|^atm$|^zaptel$|^tty$) {
...

The LED settings for the 3G mode are done via LuCI.

I tried the script in OP #1. Unfortunately, it doesnt work for me. I have a Netgear WNDr3700 router connected to an ADSL modem (wan) and a 3g UMTS stick (wan2).

When the ADSL/'wan' is up, the route looks like this

#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         113.192.16.1    0.0.0.0         UG    10     0        0 pppoe-wan
8.8.4.4         0.0.0.0         255.255.255.255 UH    10     0        0 pppoe-wan
113.192.16.1    0.0.0.0         255.255.255.255 UH    0      0        0 pppoe-wan
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 br-lan

After the disconnecting the Modem, the 3G wan2 should kick in. However, the failover script gives an error

#sh -x failover.sh
+ awk /packets received/ {print $4}
+ ping -w 2 -c 1 8.8.4.4
+ RET=
+ [  -ne 1 ]
sh: bad number
+ [ 1 = 2 ]
+ sleep 1

and the route looks like this

# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 br-lan

It seems that 8.8.4.4 is not reachable since pppoe-wan is down. Is there any way to fix this?

My network is as follows:

# uci show network
network.loopback=interface
network.loopback.ifname='lo'
network.loopback.proto='static'
network.loopback.ipaddr='127.0.0.1'
network.loopback.netmask='255.0.0.0'
network.globals=globals
network.globals.ula_prefix='fd83:8468:5ddf::/48'
network.lan=interface
network.lan.ifname='eth0.1'
network.lan.force_link='1'
network.lan.type='bridge'
network.lan.proto='static'
network.lan.ipaddr='192.168.1.1'
network.lan.netmask='255.255.255.0'
network.lan.ip6assign='60'
network.wan=interface
network.wan.ifname='eth1'
network.wan._orig_ifname='eth1'
network.wan._orig_bridge='false'
network.wan.proto='pppoe'
network.wan.username='username'
network.wan.password='password'
network.wan.metric='10'
network.wan.peerdns='0'
network.wan.dns='8.8.8.8 218.248.255.204'
network.wan6=interface
network.wan6.ifname='eth1'
network.wan6.proto='dhcpv6'
network.@switch[0]=switch
network.@switch[0].name='switch0'
network.@switch[0].reset='1'
network.@switch[0].enable_vlan='1'
network.@switch[0].blinkrate='2'
network.@switch_vlan[0]=switch_vlan
network.@switch_vlan[0].device='switch0'
network.@switch_vlan[0].vlan='1'
network.@switch_vlan[0].ports='0 1 2 3 5t'
network.@switch_port[0]=switch_port
network.@switch_port[0].device='switch0'
network.@switch_port[0].port='1'
network.@switch_port[0].led='6'
network.@switch_port[1]=switch_port
network.@switch_port[1].device='switch0'
network.@switch_port[1].port='2'
network.@switch_port[1].led='9'
network.@switch_port[2]=switch_port
network.@switch_port[2].device='switch0'
network.@switch_port[2].port='5'
network.@switch_port[2].led='2'
network.wan2=interface
network.wan2.proto='3g'
network.wan2.device='/dev/ttyUSB0'
network.wan2.apn='www'
network.wan2.dialnumber='*99#'
network.wan2.service='umts'
network.wan2.auto='0'
network.wan2.ifname='3g'
network.@route[0]=route
network.@route[0].interface='wan'
network.@route[0].target='8.8.4.4'
network.@route[0].netmask='255.255.255.255'

(Last edited by trumee on 1 Nov 2015, 16:25)

The discussion might have continued from here.