alaaibrahim / ala-scripts

Holds a collection of my scripts that makes parts of my life easier

This URL has Read+Write access

ala-scripts / checkGW
100644 52 lines (47 sloc) 1.095 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
 
# Timeout for a ping request from the machine
TIMEOUT=2
# The Array of gateways, the first one has the highest priority
DEFAULTGW=(192.168.1.1 192.168.1.2 192.168.1.3)
# The time between each check
SLEEPTIME=10
 
function switchGW {
    newRoute=$1
    /sbin/ip route change default via $newRoute
}
 
function getDefaultGW {
    /sbin/ip route | grep default | cut -d' ' -f3
}
 
function GWAlive {
    /bin/ping -W $TIMEOUT -c 1 google.com > /dev/null 2>&1
}
while :
do
GWAlive
    RET=$?
    if [ $RET -ne 0 ]
    then
echo "`getDefaultGW` is Down"
        # Loop through the gateways
        SUCCESS=0
        for i in ${DEFAULTGW[@]}; do
echo "Trying GW $i"
     switchGW $i
     GWAlive
     TEST=$?
     if [ $TEST -eq 0 ]
     then
     #This One is working
     echo "$i seams functioning"
     SUCCESS=1
     break
fi
done
if [ $SUCCESS -eq 0 ]
        then
echo "All Gateways are down, switching to $DEFAULTGW and waiting for next check"
switchGW $DEFAULTGW
        fi
fi
sleep $SLEEPTIME
done