-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnection_status.sh
More file actions
executable file
·97 lines (89 loc) · 2.58 KB
/
connection_status.sh
File metadata and controls
executable file
·97 lines (89 loc) · 2.58 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
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
91
92
93
94
95
96
97
#!/bin/bash
#
### Network connectivity checker and script trigger
#
# Requires: ping, dig
. /opt/scripts/shlog.sh
. /opt/scripts/shlock.sh
. /opt/scripts/pushbullet.sh
### IP Settings
gatewayIP=$(ip route list|grep "default via"|awk '{print $3}')
internetDNS="google.com"
internetIP="8.8.8.8"
### Post check scripts
dyndnsScript="/opt/scripts/dynamic_dns_updater.sh"
postScript="/opt/scripts/post_check_scripts.sh"
run_scripts (){
shlog -s timestamp "Running dynamic DNS updater" -p nolog
bash "$dyndnsScript"
if [ $? -eq 0 ]; then
shlog -s timestamp "Running post check scripts" -p nolog
bash "$postScript"
else
shlog -s datestamp "The DNS updater script exited with code $?. Not running post check scripts"
fi
}
check_dns (){
# Check DNS IP
dig +noall +short +time=1 +tries=1 +retry=1 $internetDNS 2>&1 >/dev/null
if [ $? -ne 0 ]; then
# Check again, since it failed
dig +noall +short $internetDNS 2>&1 >/dev/null
if [ $? -ne 0 ]; then
shlog -s datestamp "DNS check: $internetDNS - DOWN"
dnsStatus="0"
else
shlog -s datestamp "DNS check: $internetDNS - UP?"
dnsStatus="1"
fi
else
shlog -s datestamp "DNS check: $internetDNS - UP" -p nolog
dnsStatus="1"
fi
}
check_internet (){
# Check Google IP
ping -c 1 $internetIP 2>&1 >/dev/null
if [ $? -ne 0 ]; then
# Check again, since it failed
ping -c 10 $internetIP 2>&1 >/dev/null
if [ $? -ne 0 ]; then
shlog -s datestamp "IP check: $internetIP - DOWN"
internetStatus="0"
else
shlog -s datestamp "IP check: $internetIP - UP?"
internetStatus="1"
fi
else
shlog -s datestamp "IP check: $internetIP - UP" -p nolog
internetStatus="1"
fi
}
### Main
# Check Gateway IP
ping -c 1 $gatewayIP 2>&1 >/dev/null
if [ $? -ne 0 ]; then
shlog -s datestamp "Gateway IP: $gatewayIP - DOWN"
remove_lock
exit 1
else
shlog -s datestamp "Gateway IP: $gatewayIP - UP" -p nolog
check_dns
check_internet
# Compare both checks and act accordingly
if [ $dnsStatus -eq 0 ] && [ $internetStatus -eq 0 ]; then
shlog -s timestamp "Both DNS resolution and Internet IP are down in the main gateway, exiting..."
# Very unlikely that this will go through but try it anyway
pushb "Both DNS resolution and Internet IP are down in the main gateway. Not doing anything!"
remove_lock
exit 2
elif [ $dnsStatus -eq 0 ] && [ $internetStatus -eq 1 ]; then
shlog -s timestamp "DNS resolution is down but Internet IP is up. DNS resolution might fail, trying anyway..."
run_scripts
else
shlog -s timestamp "Both DNS and Internet IPs are up in the main gateway" -p nolog
run_scripts
fi
fi
remove_lock
exit 0