Hello,
Problem:
Internet connection from my DSL provider sometimes collapses. So, I am using 3G modem (best option for me) for a buck-up.
I want to configure my router to automatic switches between DSL and 3G.
When DSL connection is collapsed it switch to 3G but when is working again it switch back to DSL. But it's one condition: 3G connection cannot be used (because of costs) when DSL is working.
I tried multwan but unfortunately it isn't good for me. 3G modem is always connected to Internet and there is traffic on ppp0 interface (UDP, SIP eg. from VoIP devices) even if DSL connection it set up as a default route.
I wrote simple and ugly script solving my problem:
/etc/init.d/failover
#!/bin/sh /etc/rc.common
START=90
start() {
/usr/bin/failover.sh &
}
stop() {
PID=`ps | awk '/failover.sh/ {print $1}'`
kill -9 $PID
}
/usr/bin/failover.sh
#!/bin/sh
INTERVAL=1
PACKETS=1
HOST="8.8.4.4"
WAN1=wan
WAN2=wan2
USINGWAN=1;
LOG="/root/failover.log"
echo "`date`: Failover script started." >> $LOG
while sleep $INTERVAL
do
RET=`ping -w 2 -c $PACKETS $HOST 2>/dev/null | awk '/packets received/ {print $4}'`
if [ "$RET" -ne "$PACKETS" ]; then
if [ "$USINGWAN" = "1" ]; then
ifup $WAN2
USINGWAN=2
echo "`date`: Changed active WAN port to 3G modem!" >> $LOG
fi
else
if [ "$USINGWAN" = "2" ]; then
ifdown $WAN2
echo "nameserver 8.8.8.8" > /etc/resolv.conf
USINGWAN=1
echo "`date`: Changed active WAN port to DSL connection!" >> $LOG
fi
fi
done;
And add new route to 8.8.4.4 via DSL connection
Edit /etc/config/network add at the end:
config 'route'
option 'interface' 'wan'
option 'target' '8.8.4.4'
option 'netmask' '255.255.255.255'
Now make this files executable:
> chmod +x /etc/init.d/failover
> chmod +x /usr/bin/failover.sh
Enable and start the script
> /etc/init.d/failover enable
> /etc/init.d/failover start
Script saves log file in root's directory (/root/failover.log).
For me it usually take about 15-20 seconds to switch to 3G modem and about 5 seconds to back to DSL.
regards,
mateuszkj