-
Notifications
You must be signed in to change notification settings - Fork 2
/
watchdog.sh
executable file
·48 lines (44 loc) · 1.61 KB
/
watchdog.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
#!/bin/sh
######################################################################
# Use crontab to run this script every minute. #
# Here is a crontab template. #
# #
# * * * * * /root/watchdog.sh >> /tmp/dial_watchdog.log 2>&1 #
# #
######################################################################
logtime() {
date "+%Y-%m-%d %H:%M:%S"
}
networktest() {
ping -c 2 10.0.1.5 > /dev/null 2>&1 || return 255 # Cannot ping to dial up server
ping -c 2 114.114.114.114 > /dev/null 2>&1 || return 254 # Cannot ping to 114 DNS server
ping -c 2 baidu.com > /dev/null 2>&1 || return 1 # Can connect to 114 DNS server but not baidu.com
return 0 # Network OK
}
networktest
result=$?
if [ $result -eq 0 ]; then
echo "$(logtime) Network no problem."
exit 0
fi
if [ $result -eq 1 ]; then
echo "$(logtime) DNS configuration error."
exit 2
fi
if [ $result -eq 255 ]; then
echo "$(logtime) Network not connected! Please check your wire connection!"
exit 255
fi
if [ $result -eq 254 ]; then
echo "$(logtime) Trying to login..."
ret=$(./dial.sh login)
result=$(echo $ret | grep -o '"result":\d' | grep -o '\d')
if [[ $result == 1 ]] ; then
echo "$(logtime) Logged in successfully"
exit 0
else
msga=$(echo $ret | grep -o '"msga":".*"' | cut -d ':' -f 2 | grep -o '[^"]*')
echo "$(logtime) Logged in failed, error msg: ${msga}"
exit 254
fi
fi