Created
March 30, 2015 20:16
-
-
Save consideRatio/bb422adedbf5f7867f38 to your computer and use it in GitHub Desktop.
UpUnet.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# UpUnet-S Connectivity script | |
# 1: Keeps connection alive by continious pinging | |
# 2: Automatically reconnects to UpUnet-S using provided username/password | |
# 3: Keep you informed about changes in connectivity to... | |
# -- Router -> UpUnet-S -> Internet -> DNS Server | |
# USAGE | |
# Install coreutils (brew install coreutils) | |
# Add as a function in your .bash_profile on your UNIX system | |
# Call by writing: upunet | |
# Utilizes coreutils (brew install coreutils) | |
function upunet { | |
clear | |
echo "Monitoring and handling UpUnet-S connectivity" | |
# You must URL-encode your special characters! | |
# For example, replace + with %2B (google 'URL encode' for more info) | |
USERGROUP="UpUnet-S" | |
USER="ENTER YOUR USERNAME HERE" # Username variable busy! | |
PASSWORD="ENTER YOUR PASSWORD HERE" | |
STEP1IP="192.168.1.1" # An IP to check your connectivity to your Router | |
STEP2IP="10.10.10.10" # An IP to check your connectivity to UpUnet-S | |
STEP3IP="8.8.8.8" # An IP to check your internet access | |
PINGATTEMPTS=4 | |
PINGDELAY=0.2 | |
PINGTIMEOUT=1 | |
TIMEFORMAT="+%H:%M:%S" | |
LOGINURL="https://10.10.10.10/?action=Login&usergroup=${USERGROUP}&username=${USER}&password=${PASSWORD}" | |
WGETCALL="wget --spider --no-check-certificate" | |
MESSAGE="" | |
LATEST="?" | |
while [ true ]; do | |
STEP3=$[ $(gtimeout $PINGTIMEOUT ping -i $PINGDELAY -c $PINGATTEMPTS $STEP3IP 2>&1 | grep -c "bytes from") != 0 ] | |
if [ $STEP3 == 1 ]; then | |
# Only check DNS whenever we regain lost connectivity to internet | |
if [ $LATEST != "Full internet access" ]; then | |
DNS=$[ $(host www.google.com | grep -c "has address") != 0 ] | |
if [ $DNS != 0 ]; then | |
MESSAGE="Full internet access" | |
else | |
MESSAGE="Internet access with DNS failure" | |
fi | |
fi | |
else | |
STEP2=$[ $(gtimeout $PINGTIMEOUT ping -i $PINGDELAY -c $PINGATTEMPTS $STEP2IP 2>&1 | grep -c "bytes from") != 0 ] | |
if [ $STEP2 == 1 ]; then | |
LOGIN=$[ $(wget --spider --no-check-certificate $LOGINURL 2>&1 | grep -c '200 OK') == 1 ] | |
if [ $LOGIN == 1 ]; then | |
MESSAGE="UpUnet-S login succeeded" | |
else | |
MESSAGE="UpUnet-S login failed" | |
fi | |
else | |
STEP1=$[ $(gtimeout $PINGTIMEOUT ping -i $PINGDELAY -c $PINGATTEMPTS $STEP1IP 2>&1 | grep -c "bytes from") != 0 ] | |
if [ $STEP1 == 1 ]; then | |
MESSAGE="No internet access, can't reach UpUnet-S ($STEP2IP)" | |
else | |
MESSAGE="No internet access, can't reach Router ($STEP1IP)" | |
fi | |
fi | |
fi | |
# Notify changes only | |
if [ $MESSAGE != $LATEST ]; then | |
echo "$(date $TIMEFORMAT) - Connection: $MESSAGE" | |
LATEST=$MESSAGE | |
attention | |
fi | |
done | |
} | |
# Supposed to make small beeps in succession | |
function attention { | |
sleep 0.1 | |
echo -ne "\a" | |
sleep 0.1 | |
echo -ne "\a" | |
sleep 0.1 | |
echo -ne "\a" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment