Skip to content

Instantly share code, notes, and snippets.

@gardner
Created November 3, 2015 13:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save gardner/9a583ebdef6f41ea09cb to your computer and use it in GitHub Desktop.
Save gardner/9a583ebdef6f41ea09cb to your computer and use it in GitHub Desktop.
OpenWrt cron job to check the connection and, if there is no connection, reboot the Huawei E3272 modem using the hilink api.
#!/bin/ash
# OpenWrt cron job to check the connection and, if there is no connection,
# reboot the Huawei E3272 modem using the hilink api. This script is optimized
# for busybox and written in ash.
# Put this script in /bin/check_connection and then: chmod +x /bin/check_connection
# Then: crontab -e
# */15 * * * * /bin/check_connection > /tmp/reboot_con.log 2>&1
f="/tmp/reboot_lock"
if [ ! -f "$f" ]; then
echo "last reboot: never"
touch "$f"
fi
last_reboot=`date -r $f +%s`
let difference=`date +%s`-$last_reboot
echo "last reboot: $difference seconds ago";
if [ $difference -lt 900 ]; then
echo "reboot occurred within the last 15 minutes. bailing."
exit 0
fi
alias reboot_modem='curl '\''http://192.168.1.1/api/device/control'\'' -H '\''Origin: http://192.168.1.1'\'' -H '\''Content-Type: application/x-www-form-urlencoded; charset=UTF-8'\'' -H '\''Accept: */*'\'' -H '\''Referer: http://192.168.1.1/html/reboot.html'\'' -H '\''X-Requested-With: XMLHttpRequest'\'' -H '\''Connection: keep-alive'\'' --data '\''<?xml version="1.0" encoding="UTF-8"?><request><Control>1</Control></request>'\'' '
ping -W 60 -s 2000 8.8.8.8 -c 1 > /dev/null
if [ "$?" -eq "0" ]; then
echo "connected";
exit 0;
else
echo "connection timed out after 60 seconds. trying again...";
ping -W 60 -s 2000 8.8.8.8 -c 1 > /dev/null
if [ "$?" -eq "0" ]; then
echo "reconnected";
exit 0;
else
echo "still disconnected. initiating reboot...";
touch "$f"
reboot_modem
sleep 60
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment