-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_manager.sh
executable file
·83 lines (74 loc) · 1.5 KB
/
connection_manager.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
#!/bin/bash
#
# Script to call wvdial and continuosly check
# the connection. If its lost, redial
#
CONF=tmobile
LOGF=/var/log/connection.log
LOGCMD="logger -s"
T0=0
T1=0
i=0
connect() {
$LOGCMD "Starting wvdial connection"
[ -n ${CONF} ] && wvdial ${CONF} &
[ $? -eq 1 ] && $LOGCMD "Modem not responding"
sleep 8
WPID=$(pgrep wvdial)
PPPPID=$(pgrep pppd)
[ $? -ne 0 ] && connect || $LOGCMD "Connection stabilished"
}
reconnect() {
$LOGCMD "Reconnection"
terminate
connect
}
terminate() {
$LOGCMD "Killing connection"
kill -9 ${PPPPID} ${WPID} &> /dev/null
sleep 2
PPPPID=$(pgrep pppd)
[ -n $? ] && [ $? -ne 0 ] && terminate
}
update_diff_counter() {
# check for received data segments
T0=$(netstat -st|grep -A 8 Tcp:|grep "segments received"|cut -d" " -f 5)
ping -c2 google.com &> /dev/null
sleep 4
let T1=$(netstat -st|grep -A 8 Tcp:|grep "segments received"|cut -d" " -f 5)-$T0
}
check() {
# 1st check: Are the commands running?
WPID=$(pgrep wvdial)
PPPPID=$(pgrep pppd)
if [ -z ${PPPPID} ] || [ -z ${WPID} ] ; then
$LOGCMD "pppd and/or dialer not running"
RET=1
# 2nd check: the connection is really up?
elif [ $T1 -eq 0 ]; then
let i++
if [ $i -eq 15 ]; then
i=0
RET=1
elif [ $i -gt 20 ]; then
RET=2
else
RET=0
fi
else
i=0
RET=0
fi
}
# Start...
touch $LOGF
connect
# Main loop
while true; do
# different treatment to different problems
update_diff_counter
check
echo T0 $T0 - T1 $T1 - i $i
[ $RET -eq 1 ] && reconnect
[ $RET -eq 2 ] && echo REBOOT
done