-
Notifications
You must be signed in to change notification settings - Fork 1
/
default-gw.sh
executable file
·90 lines (72 loc) · 2.17 KB
/
default-gw.sh
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
function change_router {
if [[ $STATE = "down" ]] ; then
echo -e "\n\n\nstate change $STATE down \n!!!!!!!!!!!!!!!!!!!!!!" # >> /var/log/default-gw.log
route -n
while route -n | grep ^0.0.0.0 | grep -q $SECOND_GW ; do
route del default gw $SECOND_GW dev $SECOND_INT
done
route add default gw $SECOND_GW dev $SECOND_INT metric 0
while route -n | grep ^0.0.0.0 | grep -q $MAIN_GW ; do
route del default gw $MAIN_GW dev $MAIN_INT
done
route add default gw $MAIN_GW dev $MAIN_INT metric 1
route -n
echo -e "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n" # >> /var/log/default-gw.log
else
echo -e "\n\n\nstate change $STATE work\n!!!!!!!!!!!!!!!!!!!!!!!" # >> /var/log/default-gw.log
route -n
while route -n | grep ^0.0.0.0 | grep -q $MAIN_GW ; do
route del default gw $MAIN_GW dev $MAIN_INT
done
route add default gw $MAIN_GW dev $MAIN_INT metric 0
while route -n | grep ^0.0.0.0 | grep -q $SECOND_GW ; do
route del default gw $SECOND_GW dev $SECOND_INT
done
route add default gw $SECOND_GW dev $SECOND_INT metric 1
route -n
echo -e "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n" # >> /var/log/default-gw.log
fi
}
function check_router {
int=$1
count=0
orig_count=$(echo $CHECK_LIST | awk '{print NF}')
for i in $CHECK_LIST ; do
if ping -I $int -c 1 -W 1 -w 1 -q $i > /dev/null ; then
(( count++ ))
fi
done
if (( count < ( orig_count / 2 ) )) ; then
return 1 #вернем false, так как ниодин пинг не вернулся
else
return 0 #вернем true, так как хотя бы один пинг вернулся
fi
}
CHECK_LIST="www.ru ya.ru mail.ru google.ru vk.com rambler.ru yahoo.com"
MAIN_INT=eth0 #основной интерфейс
SECOND_INT=wlan0 #вторичный интерфейс
MAIN_GW="10.10.1.1"
SECOND_GW="192.168.2.1"
STATE="work"
OLD_STATE=$STATE
#####################
#main function
while true ; do
#echo "new iteration start"
sleep 5
#echo -e "\n!!!!!!!!!!!!!!!!"
#echo $STATE
#echo -e "!!!!!!!!!!!!!!!!\n"
if check_router $MAIN_INT ; then
STATE="work"
else
STATE="down"
fi
if [[ $STATE = $OLD_STATE ]] ; then
continue
else
change_router
OLD_STATE=$STATE
fi
done