-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-hs100.sh
96 lines (83 loc) · 2.78 KB
/
check-hs100.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
91
92
93
94
95
96
#!/bin/bash
# Usage:
# Intended to be used as a cron job with flock, which is a core Linux utility.
# I use it with the following, which will run every minute of every day:
# * * * * * flock -n /tmp/check.lock /path/to/check.sh
# This will find the first five hops, find rows that have IPs, take anything
# from hop 2 to 9 (eg - past my router and modem) then grab the IP address of the
# first hop outside our network. Modify for what suits your needs.
firsthop=`traceroute 8.8.8.8 -n -m 5 | grep -E "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | grep -m 1 -E "^\s?\b[3-9]{1,2}" | tr -s " " | cut -d" " -f3`
failures=0
# I have 0 faith that the firsthop script will always return due
# to my provider, so I'm using a fall back to OpenDNS primary.
if [ -z $firsthop ];
then
firsthop="208.67.220.220"
fi
echo "First hop is: $firsthop"
# I am using 3 tests from 3 providers to see if it's an issue with
# the firsthop or multiple servers. Here's the servers I'm using:
# 1. My first hop or OpenDNS primary
# 2. Google Public DNS primary
# 3. Dyn Public DNS primary
# I use IPs on the off chance that there's a DNS issue.
tests=($firsthop "8.8.8.8" "216.146.35.35")
for test in "${tests[@]}";
do
echo "Testing $test"
ping -c 1 $test
if [ $? -ne 0 ];
then
let "failures += 1"
fi
done
if [ $failures -eq 3 ];
then
# 1 set of tests was too gittery
failures=0
for test in "${tests[@]}";
do
echo "Testing $test"
ping -c 1 $test
if [ $? -ne 0 ];
then
let "failures += 1"
fi
done
if [ $failures -eq 3];
then
# Init lastfailure with some time (so we don't fail later) if it
# does not exist
if ! [ -f /tmp/lastfailure ];
then
date +%s --date "7 hours ago" > /tmp/lastfailure
fi
# Get last failure time and find out how many minutes ago it was
# if it was less than 30 minutes ago, we probably have something
# else going on. Restart every 30 minutes until something happens.
let lastfailure=" (`date +%s`-`tail -n1 /tmp/lastfailure`)/60 "
if [ $lastfailure -ge 30 ];
then
date +%s >> /tmp/lastfailure
echo "Last failure was $lastfailure minutes ago. Restarting."
# Turn the HS100 off, wait 10 seconds, then turn it back on
# Wait 10 minutes because it takes a while for my internet
# to come back up.
# Shout out to ggeorgovassilis for the hs100.sh scripts:
# https://github.com/ggeorgovassilis/linuxscripts/blob/master/tp-link-hs100-smartplug/hs100.sh
/usr/local/bin/hs100.sh 192.168.1.60 9999 off
sleep 10s
/usr/local/bin/hs100.sh 192.168.1.60 9999 on
sleep 10m
else
echo "Last failure was $lastfailure minutes ago. Not restarting."
fi
fi
else
if [ $failures -ne 0 ];
then
echo "There were $failures failures. There's an issue, but the internet isn't down."
else
echo "There are no issues."
fi
fi