#! / bin / bash
host = $ {host: = "4.2.2.2"}
router_ip = "192.168.1.1"
router_reboot_path = "/ rebootinfo.cgi"
router_user = "admin"
router_pw = "pw_here"
num_packets = 4
receiving = ""
reboots = 1
connect_wait = 60
while ["$ receiving" == ""]
do
echo -ne `date +% T`" \ t "
echo "pinging $ host with $ num_packets packets"
receiving = `ping $ host -c $ num_packets | grep "$ num_packets received" `
if ["$ receiving"! = ""]
then
echo -ne `date +% T`" \ t "
echo "$ host is alive"
else
echo -ne `date +% T`" \ t "
echo "$ host not responding"
echo -ne `date +% T`" \ t "
echo -n "reboot router - attempt $ reboots"
let "reboots + = 1"
`curl -s -u $ router_user: $ router_pw" http: // $ router_ip $ router_reboot_path "> / dev / null`
sleep 10
routerAlive = ""
while ["$ routerAlive" == ""]
do
routerAlive = `ping $ router_ip -c $ num_packets | grep "$ num_packets received" `
if ["$ routerAlive"! = ""]
then
echo "done."
echo -ne `date +% T`" \ t "
echo -n "waiting $ connect_wait sec for possible connect"
sec = 0
while ["$ sec"! = $ connect_wait]
do
let "sec + = 1"
if ["$ sec" == $ connect_wait]
then
echo "."
else
echo -n "."
fi
sleep 1
done
else
echo -n "."
fi
done
fi
done
$ ./routerReboot 23:25:48 pinging 4.2.2.2 with 4 packets 23:26:01 4.2.2.2 not responding 23:26:01 reboot router - attempt 1 ..... done. 23:27:10 waiting 60 sec for possible connect ....................................... ..................... 23:28:11 pinging 4.2.2.2 with 4 packets 23:28:14 4.2.2.2 is alive
Source: https://habr.com/ru/post/99245/
All Articles