"Fossies" - the Fresh Open Source Software Archive 
Member "aif-2.1.1/contrib/adsl-failover" (16 Sep 2020, 6968 Bytes) of package /linux/privat/aif-2.1.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the latest
Fossies "Diffs" side-by-side code changes report for "adsl-failover":
2.1.0_vs_2.1.1.
1 #!/bin/bash
2
3 # This script was written by Mark Cleverdon 22/11/2007 under the GPL license
4 # email: mark at lanzarote1.com
5 #
6 # For automatic Failover from the main external network provider on failure
7 # to a dialup modem or UMTS modem connection.
8 #
9 # REQUREMENTS
10 # You must have IP forwarding enabled in the system.
11 # ADSL connection with a static IP
12 # wvdial must be installed and setup with a script that works for your service provider.
13 # A firewall like arnos iptables firewall script or shorewall etc
14 #
15 # You can set this script in the system cron to check the connection at regular time intervals.
16 #
17 # METHODS USED IN THIS SCRIPT
18 # We have both eth1 and ppp0 as external interfaces in iptables, if both were functioning at the same time
19 # we would have load balancing (which could get expensive), but here we only need one active connection so
20 # all we need to do is to adjust the dynamic IP address of the dialup connection in the firwall when there
21 # is a problem in order to allow routing through the dialup device. Once the emergency is over we can can-
22 # cel the dialup connection and the original routing will continue as usual with no need to adjust
23 # the firewall again. When a new failure happens then the new IP address is assigned to the second external
24 # interface (normally ppp0) and the firewall restarted to allow routing.
25 #
26 # PARAMETERS HERE
27 # The normal external interface (DSL or cable etc)
28 interface="eth1"
29 normal_ext_ip="XXX.XXX.XXX.XXX"
30 # Admin email address for failover notification
31 admin_email="admin@your-domain"
32 # The dialup device PCMCIA UMTS card or regular modem will normally be a ttyS0/1/2/3 etc
33 # But here you need the device that wvdial returns which is generally ppp0
34 dialup_if="ppp0"
35 # Full path and name of your firewall script I use Arnos iptables firewall
36 firewall="/etc/arno-iptables-firewall/firewall.conf"
37 # The /etc/init.d/firewall-script that your system uses to restart the firewall
38 firestarter="/etc/init.d/arno-iptables-firewall"
39 # WARNING if you are not using arnos firewall script you will need to edit the sed commands parameters below
40 # eg. on line 111 of this script
41 # sed 's/\(search string just before substitution \)[0-9]*.[0-9]*.[0-9]*.[0-9]*/\1'$OUT_IP'/g' $firewall
42 #
43 # The wvdial command for dialup (you must set this up previously)
44 # eg. this would be like "myserver:#/wvdial internet" on the command line
45 connection="internet"
46 # The following are the ip address of any reliable public server ie. google.com
47 # if all three servers fail then the dialup connection will be started.
48 # But beware of ping_server1, it must be the most reliable of the three because
49 # it is used in further tests on its own.
50 # Further, note it is important to use IP addresses and not domain names because if
51 # your system is totally isolated you will not be able to do name resolution.
52 ping_server1="64.233.167.99"
53 ping_server2="216.109.112.135"
54 ping_server3="66.45.254.244"
55
56 route add $ping_server1 gw $normal_ext_ip
57 if (ping -w 5 -nq -I $interface $ping_server1 |grep '100%\ packet\ loss' 2>&1>/dev/null) then
58 SERVER1="DOWN"
59 else
60 SERVER1="UP"
61 fi
62 route del $ping_server1
63 route add $ping_server2 gw $normal_ext_ip
64 if (ping -w 5 -nq -I $interface $ping_server2 |grep '100%\ packet\ loss' 2>&1>/dev/null) then
65 SERVER2="DOWN"
66 else
67 SERVER2="UP"
68 fi
69 route del $ping_server2
70 route add $ping_server3 gw $normal_ext_ip
71 if (ping -w 5 -nq -I $interface $ping_server3 |grep '100%\ packet\ loss' 2>&1>/dev/null) then
72 SERVER3="DOWN"
73 else
74 SERVER3="UP"
75 fi
76 route del $ping_server3
77
78
79 if [ "$SERVER1" = "DOWN" ] && [ "$SERVER2" = "DOWN" ] && [ "$SERVER3" = "DOWN" ]
80 then
81 echo "WARNING! -- We have an internet connection problem. I will attempt to discover the situation and fix it."
82 if [[ "$interface"="eth1" ]]
83 then
84 echo .
85 # Check to see if we have any connection at all (if not we dial)
86 if (ping -w 5 -nq $ping_server1|grep '100%\ packet\ loss' 2>&1>/dev/null)
87 then
88 # Check for existing wvdial processes that may be hungup
89 if (ps ax|grep wvdial)
90 then
91 echo "finishing any previous hung connection"
92 killall wvdial
93 sleep 15
94 fi
95 echo .
96 echo "Dialing out ...."
97 wvdial $connection &
98 sleep 25
99 if (ping -w 5 -nq -I $dialup_if $ping_server1|grep -v '100%\ packet\ loss' 2>&1>/dev/null)
100 then
101 # Pick up the new IP for adjusting the firewall script.
102 OUT_IP=`ifconfig|grep -A 2 $dialup_if|grep inet\ addr:|sed 's/.*inet\ addr\:\([0-9]*.[0-9]*.[0-9]*.[0-9]*\).*/\1/g'`
103 echo .
104 echo "New public IP address is <$OUT_IP> from UMTS/dialup device $dialup."
105 echo .
106 echo "Restarting the Firewall routing for new connection."
107 echo .
108 # Make sure the original firewall script is saved (here we datestamp it just in case)
109 cp -p $firewall $firewall.`date +"%d%m%Y%H%M%S"`
110 sed 's/\(NAT_STATIC_IP="$normal_ext_ip \)[0-9]*.[0-9]*.[0-9]*.[0-9]*/\1'$OUT_IP'/g' $firewall >/tmp/firewall.conf
111 mv /tmp/firewall.conf $firewall
112 $firestarter restart
113 # Get myself a copy of all this stuff for a remote firewall setup etc.
114 echo "The ADSL connection has failed">/tmp/ifconfig_ppp0
115 echo ".">>/tmp/ifconfig_ppp0
116 echo "The new IP address is $OUT_IP">>/tmp/ifconfig_ppp0
117 ifconfig $dialup_if>>/tmp/ifconfig_ppp0
118 echo $OUT_IP>/tmp/FAILOVER_IP
119 mail -s "FAILOVER IP $OUT_IP" $admin_email </tmp/ifconfig_ppp0
120 else
121 echo "UMTS/Dialup connection has failed, I will kill it. Please try again."
122 killall wvdial
123 fi
124 else
125 # Failover is working
126 echo .
127 echo "Failover system is already working"
128 OUT_IP1=`cat /tmp/FAILOVER_IP`
129 OUT_IP2=`ifconfig|grep -A 2 $dialup_if|grep inet\ addr:|sed 's/.*inet\ addr\:\([0-9]*.[0-9]*.[0-9]*.[0-9]*\).*/\1/g'`
130 # Check to see if dynamic ip has changed
131 if [ "$OUT_IP1" = "$OUT_IP2" ]
132 then
133 echo $OUT_IP2>/tmp/FAILOVER_IP
134 echo "The ADSL connection has failed">/tmp/ifconfig_ppp0
135 echo ".">>/tmp/ifconfig_ppp0
136 echo "The new IP address is $OUT_IP2">>/tmp/ifconfig_ppp0
137 echo ".">>/tmp/ifconfig_ppp0
138 ifconfig $dialup_if>>/tmp/ifconfig_ppp0
139 mail -s "NEW FAILOVER IP $OUT_IP2" $admin_email </tmp/ifconfig_ppp0
140 fi
141 fi
142 fi
143 echo "Through DSL connection $ping_server1 is $SERVER1, $ping_server2 is $SERVER2, $ping_server3 is $SERVER3"
144 else
145 # Everything is Normal.
146 # Check if Normality is recent and if so kill off all failover devices.
147 if (ps ax|grep wvdial|grep -v grep)
148 then
149 echo "ADSL is now OK, I will kill any UMTS/Dialup connection..."
150 killall wvdial 2>&1>/dev/null
151 echo "The ADSL connection is now working">/tmp/ifconfig
152 echo ".">>/tmp/ifconfig
153 echo "Use the original connection information to connect">>/tmp/ifconfig
154 echo ".">>/tmp/ifconfig
155 mail -s "FAILOVER STOPED - ADSL OK" $admin_email </tmp/ifconfig
156 echo "Through DSL connection $ping_server1 is $SERVER1, $ping_server2 is $SERVER2, $ping_server3 is $SERVER3"
157 ifdown eth1
158 sleep 10
159 ifup eth1
160 fi
161 fi