-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlost-reboot-dig3.sh
51 lines (42 loc) · 1.24 KB
/
lost-reboot-dig3.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
#!/bin/bash
servers=("google.com" "yahoo.com" "facebook.com") #Servers to query, change as needed.
failed_count=0
PING_IP_ADDRESS=x.x.x.x #Change IP as needed.
DNS_IP=1.1.1.1 #Uses Cloudflare by default, change as needed.
function check_server() {
if dig +short @$DNS_IP "$1" >/dev/null; then
echo "Server $1 is reachable."
return 0
else
echo "Server $1 is unreachable."
return 1
fi
}
function restart_sleep() {
failed_count=0
sleep 30
}
function reboot_if_needed() {
if ping -c 1 "$PING_IP_ADDRESS" >/dev/null; then
echo "The internet connection is down"
else
echo "At least 2 out of 3 servers failed. Rebooting..."
sudo reboot
fi
}
sleep 30 # Line for giving time for DHCP to work correctly.
while true; do
for server in "${servers[@]}"; do
if ! check_server "$server"; then
((failed_count++))
fi
done
if [ "$failed_count" -eq 1 ]; then
echo "At least 2 servers are reachable. Waiting 60 seconds before the next check..."
elif [ "$failed_count" -ge 2 ]; then
reboot_if_needed
else
echo "All servers are reachable. Waiting 60 seconds before the next check..."
fi
restart_sleep
done