Skip to content

Instantly share code, notes, and snippets.

@ivan4th
Created December 27, 2015 00:05
Show Gist options
  • Save ivan4th/b7565e6e5fd973d18b9e to your computer and use it in GitHub Desktop.
Save ivan4th/b7565e6e5fd973d18b9e to your computer and use it in GitHub Desktop.
Network connection watcher script
#!/bin/bash
set -u -e
exec >> /tmp/conncheck.log
exec 2>&1
router_ip="192.168.20.1"
ping_ip="8.8.8.8"
admin_pw="$(echo '.....insert base64 encoded pw here .....'|base64 -d)"
check_interval=300
recheck_count=10
recheck_interval=40
router_recheck_interval=5
check_net () {
ping -c 1 "$ping_ip"
}
check_router () {
ping -c 1 "$router_ip"
}
reboot_router () {
time (sleep 10; echo -e 'admin'; sleep 3; echo "$admin_pw"; sleep 5; echo 'system reboot') |
nc -q 10 "$router_ip" 23
}
while true; do
echo "*** conn check @ $(date)"
if ! check_net; then
ok=
echo "*** suspected connection failure"
for ((i = 0; i < 3; i++)); do
if check_net; then
ok=y
break
fi
done
if [ -z "$ok" ]; then
router_ok=y
while ! check_router; do
echo "*** cannot contact the router"
sleep $router_recheck_interval
router_ok=
done
if [ -z "$router_ok" ]; then
# perhaps the router was down, let it reboot
sleep $recheck_interval
fi
if [ -n "$router_ok" ] || ! check_net; then
echo "*** rebooting the router"
reboot_router
ok=
for ((i = 0; i < recheck_interval; i++)); do
echo "*** waiting for network connection"
sleep $recheck_interval
if check_net; then
echo "*** success"
ok=y
break
fi
done
if [ -n "$ok" ]; then
continue
fi
fi
fi
fi
sleep $check_interval
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment