Skip to content

Instantly share code, notes, and snippets.

@thexavier666
Last active August 31, 2020 19:26
Show Gist options
  • Save thexavier666/490799f6dd406ac5a0d2da3c3e4ac4e5 to your computer and use it in GitHub Desktop.
Save thexavier666/490799f6dd406ac5a0d2da3c3e4ac4e5 to your computer and use it in GitHub Desktop.
A simple script which allows you to automatically login if it detects a connection to the local gateway
#!/bin/bash
#-- CONFIGURATION AREA ----------------------------
# Please fill the following details, which is just
# after this comment section, before running this
# script. Otherwise, you can't login
#
# 1. GATEWAY_IP
# 2. USER (Your Alliance login username)
# 3. PASSWD (Your Alliance login password)
#
# Note: Don't share this file with ANYONE
# once you have filled your username and password
GATEWAY_IP=""
USER=""
PASSWD=""
#-- DON'T CHANGE THESE VALUE ----------------------
#-- UNLESS YOU KNOW WHAT YOU ARE DOING ------------
NET_CHK_IP="1.1.1.1"
SHORT_SLEEP=5
LONG_SLEEP=15
PING_WAIT_DUR=5
PING_NUM_MSG=3
LOGIN_URL="http://$GATEWAY_IP/0/up/"
DT_FMT="+%H:%M:%S on %d/%m/%Y"
#-- COLOR CODES TO MAKE THE OUTPUT PRETTY ---------
CLR_NULL="\e[0m"
CLR_BLK="\e[30m"
CLR_WHT="\e[97m"
CLR_BK_RED="\e[101m"
CLR_BK_BLU="\e[44m"
CLR_BK_YLW="\e[43m"
CLR_BK_GRN="\e[42m"
CLR_BK_MGN="\e[100m"
#--------------------------------------------------
# Function to check connectivity
# #1 number of ping messages
# #2 wait time per ping message
# #3 ping server
CHECK_CON()
{
SUM=0
for (( i=1; i<=$1; i++ ))
do
ping -c 1 -W $2 $3 >> /dev/null 2>&1
SUM=$(( $SUM+$? ))
done
if [ $SUM -eq $1 ]
then
# no connection
return 1
else
# connection exists
return 0
fi
}
#--------------------------------------------------
while [ true ]
do
while [ true ]
do
# checking if local gateway is reachable
CHECK_CON $PING_NUM_MSG $PING_WAIT_DUR $GATEWAY_IP
# if not reachable, keep on checking
if [ $? -eq 1 ]
then
CON_TIME=$(date "$DT_FMT")
echo -e "${CLR_BK_RED}${CLR_WHT}[ERROR] :${CLR_NULL} LAN disconnected at $CON_TIME. Retrying..."
sleep $SHORT_SLEEP
else
#echo -e "${CLR_BK_BLU}${CLR_BLK}[INFO] :${CLR_NULL} LAN connected"
break
fi
done
# checking if Internet is reachable
CHECK_CON $PING_NUM_MSG $PING_WAIT_DUR $NET_CHK_IP
# if external site not reachable, logging into ISP
if [ $? -eq 1 ]
then
CON_TIME=$(date "$DT_FMT")
echo -e "${CLR_BK_MGN}${CLR_WHT}[ERROR] :${CLR_NULL} Logged out in at $CON_TIME. Trying to login..."
curl -ksS -X POST -d "login=LOGIN&user="${USER}"&pass="${PASSWD}"" "${LOGIN_URL}" > /dev/null 2>&1
CON_TIME=$(date "$DT_FMT")
echo -e "${CLR_BK_YLW}${CLR_BLK}[INFO] :${CLR_NULL} Logged in at $CON_TIME"
else
#echo -e "${CLR_BK_GRN}${CLR_BLK}[INFO] :${CLR_NULL} Internet connected"
sleep $LONG_SLEEP
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment