├── failover
├── disc
├── mode
└── failover.sh
├── www
├── sconn
└── status.html
└── README.md
/failover/disc:
--------------------------------------------------------------------------------
1 | 0
2 |
--------------------------------------------------------------------------------
/failover/mode:
--------------------------------------------------------------------------------
1 | 1
2 |
--------------------------------------------------------------------------------
/www/sconn:
--------------------------------------------------------------------------------
1 | connected
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Simple Openwrt Failover Script
2 |
3 | ### How to install?
4 | 1. Download _.zip_ files
5 | > ``` wget https://github.com/GTANAdam/openwrt-wan-failover-script/archive/refs/tags/Release.zip ```
6 | 2. Extract _.zip_ file in **/tmp** folder
7 | > ```cd /tmp```
8 | > ``` unzip Release.zip ```
9 | 4. Move files in **www** folder to **/www** folder
10 | > ``` mv www/ /www/ ```
11 | 5. Move folder **failover** to **/etc/**
12 | > ``` mv failover /etc/failover/ ```
13 |
14 | ### How to configure script?
15 | 1. Edit **failover.sh**
16 | > ``` vi /etc/failover/failover.sh ```
17 | 1. Edit variables between comments **_# Begin Configuration #_** and **_# Begin Configuration #_**
18 |
19 | ### How to start script?
20 | 1. Configure your crontab file
21 | > ``` */2 * * * * /etc/failover/failover.sh ```
22 | 1. Restart crontab service
23 | > ``` /etc/init.d/cron reload ```
24 |
--------------------------------------------------------------------------------
/www/status.html:
--------------------------------------------------------------------------------
1 |
2 |
Connection status
3 |
14 |
15 |
16 |
17 |
36 |
37 |
--------------------------------------------------------------------------------
/failover/failover.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | # Begin Configuration #
4 | PACKETS=10
5 | PACKETS_MEDIUM=3
6 | WAIT=10
7 | HOST="8.8.4.4"
8 |
9 | WAN1Interface=eth1
10 | WAN1=wan
11 |
12 | WAN2Interface=wlan0
13 | WAN2=wwan
14 |
15 | LOG="/var/log/failover.log"
16 | STATUS="/www/sconn"
17 |
18 | MODE_PATH=/etc/failover/mode
19 | DISC_PATH=/etc/failover/disc
20 | # End Configuration #
21 |
22 | if [ -e $MODE_PATH ]; then
23 | MODE=`cat $MODE_PATH`
24 | else
25 | touch $MODE_PATH
26 | echo "1" > $MODE_PATH
27 | MODE=`cat $MODE_PATH`
28 | fi
29 |
30 | if [ -e $DISC_PATH ]; then
31 | DISC=`cat $DISC_PATH`
32 | else
33 | touch $DISC_PATH
34 | echo "0" > $DISC_PATH
35 | DISC=`cat $DISC_PATH`
36 | fi
37 |
38 | echo "`date`: Failover script started." >> $LOG
39 |
40 | # Ping on WAN1 interface
41 | RET=`ping -w $WAIT -c $PACKETS -I $WAN1Interface $HOST 2>/dev/null | awk '/packets received/ {print $4}'`
42 |
43 | #menor ou igual e maior ou igual
44 | if [ "$RET" -le "$PACKETS" -a "$RET" -ge "$PACKETS_MEDIUM" ]; then # Check if packets don't arrive on WAN1 interface
45 | if [ "$MODE" = "2" ]; then # If Mode is set to WAN2 then switch to WAN1
46 | #ifdown $WAN2
47 | uci set network.$WAN2.metric='1'
48 | uci set network.$WAN1.metric='0'
49 | uci commit network
50 | /etc/init.d/network reload
51 | echo "1" > $MODE_PATH
52 | echo "`date`: Switched to Ethernet connection!" >> $LOG
53 | echo "connected" > $STATUS
54 | fi
55 | else # If packets arrive on WAN1 interface
56 | if [ "$MODE" = "1" ]; then # If Mode is set to WAN1 then switch to WAN2
57 | #ifup $WAN2
58 | uci set network.$WAN2.metric='0'
59 | uci set network.$WAN1.metric='1'
60 | uci commit network
61 | /etc/init.d/network reload
62 | echo "2" > $MODE_PATH
63 | echo "0" > $DISC_PATH
64 | echo "`date`: Switched to Wireless connection!" >> $LOG
65 | echo "failover" > $STATUS
66 | fi
67 |
68 | # Ping on WAN2 interface
69 | RET2=`ping -w $WAIT -c $PACKETS -I $WAN2Interface $HOST 2>/dev/null | awk '/packets received/ {print $4}'`
70 | if [ "$RET2" -ne "$PACKETS" ]; then # Check if packets don't arrive on WAN2 interface
71 | if [ "$DISC" = "0" ]; then # Check if connection is set to 'outage'
72 | echo "1" > $DISC_PATH
73 | echo "disconnected" > $STATUS
74 | fi
75 | else
76 | if [ "$DISC" = "1" ]; then # Check if connection is not set to 'outage'
77 | echo "0" > $DISC_PATH
78 | echo "failover" > $STATUS
79 | fi
80 | fi
81 | # Re-up interface to avoid non-responsive gateway
82 | #ifconfig $WAN1Interface down
83 | #ifconfig $WAN1Interface up
84 | sleep 10
85 | fi
--------------------------------------------------------------------------------