Skip to content

Instantly share code, notes, and snippets.

@fbradyirl
Last active March 9, 2016 12:39
Show Gist options
  • Save fbradyirl/b43a3c84fd2c6c412bc9 to your computer and use it in GitHub Desktop.
Save fbradyirl/b43a3c84fd2c6c412bc9 to your computer and use it in GitHub Desktop.
OpenWRT internet connection script
#! /bin/sh
# Checks if the internet connection is up.
# If not, then unbind and rebind the USB port, to 'reboot' the 4G LTE stick.
# If the internet is still down after that then ping Threes 'Out of Credit' page.
# This is hosted on AWS and doesnt respond to pings, so I use wget instead.
# By doing this, you can see if you can connect to your provider, but not the rest of the internet.
# You can then know that you have perhaps run out of credit on your 4G account.
# and send a notification to yourself to topup!
# For notifications, I get the connected SIM to send an SMS to myself
# and also send the message to myself via Cisco Spark
ROOTER=/usr/lib/rooter
ROOT_DIR=/root
LOG_PREFIX="check_connection.sh:"
dns_google=8.8.8.8
dns_opendns=208.67.222.222
# Keep this up to date with IP from http://services.3apps.ie/out-of-credit.html
three_out_of_credit_url=http://54.216.114.87/out-of-credit.html
# Cisco Spark Config. Fill this in for yourself. You can get these details at https://developer.ciscospark.com/getting-started.html
spark_auth_token=1234
spark_person_uuid=1234
notify () {
# Send a message to me via Cisco Spark
curl -k https://api.ciscospark.com/v1/messages -X POST -H "Authorization:Bearer $spark_auth_token" --data "toPersonId=$spark_person_uuid" --data-urlencode "text=$1"
# Send myself a text message
export SMS_MSG="$1"
gcom -d /dev/ttyUSB0 -v -e -s $ROOT_DIR/sendsms.comgt
}
log () {
DATE=$(date +%c)
logger -t "$LOG_PREFIX" "$1"
# Also log to the ROOter log
$ROOTER/log/logger "$LOG_PREFIX $1"
echo "$DATE $1" >> $ROOT_DIR/connection_check.log
if [ $2 -eq 1 ]
then
notify "$1"
fi
}
if ping -c 1 $dns_google > /dev/null
then
echo nothing > /dev/null
echo "INFO: I can ping Google DNS. Internet check passed"
else
if ping -c 1 $dns_opendns > /dev/null
then
log "[WARNING]: I cannot ping Google DNS. But I can ping OpenDNS. Internet check passed"
else
log "[ERROR]: Cannot ping Google DNS or OpenDNS, so power toggling the USB port..."
logread > last_disconnect.log
echo "usb1" > /sys/bus/usb/drivers/usb/unbind
log "[INFO]: USB post has been unbound"
sleep 15
log "[INFO]: Binding usb port again..."
echo "usb1" > /sys/bus/usb/drivers/usb/bind
log "[INFO]: USB post has been binded again. Waiting 2 minutes for connection to come back..."
sleep 20
# Ping DNS with a timeout of 2 mins
if ping -c 1 -W 120 $dns_google > /dev/null
then
log "[INFO]: After USB power toggle, I am now once again connected. Internet check passed" 1
else
log "[ERROR]: After USB power toggle, I still Cannot ping Google DNS, so rebooting..." 1
if wget -q "$three_out_of_credit_url" > /dev/null; then
echo "I can access $three_out_of_credit_url"
log "[INFO] FYI I can access Three's $three_out_of_credit_url You are probably out of credit!" 1
else
log "[INFO] I cannot access $three_out_of_credit_url" 1
fi
reboot -f
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment