Skip to content

Instantly share code, notes, and snippets.

@z2z
Created December 25, 2012 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save z2z/4373070 to your computer and use it in GitHub Desktop.
Save z2z/4373070 to your computer and use it in GitHub Desktop.
#!/bin/bash
#ishan dot karve at gmail dot com
#Script to emulate HTTP Client login method for 24Online
#Oct 2012, Jamnagar, Gujarat
#ver 0.2 alpha
#Per User Client
#Change variables below to reflect ur setup
#Change Log
# Better program loop
# Coloured debug
# Added byte counter
##############################################################################
#define variables
tput clear
srv_ip="195.100.100.1"
username="ishankarve"
password="ishankarve"
connected=0
lastcheck_ts=0
first_time=1
interface="eth0"
#check whether curl is installed or not
#
which curl &> /dev/null
if [ $? -ne 0 ]
then
tput setaf 1 && tput bold
echo "This script heavily depends on curl for proper execution.Please install curl and try again"
exit
tput sgr0
fi
logout_session()
# logout function
{
get_tstamp
tput setaf 6
echo "$tstamp: Disconnecting now.. pls wait"
tput sgr0
#disconnect client
url="http://$srv_ip/24online/servlet/CyberoamHTTPClient"
post_data="mode=193&isAccessDenied=null&url=null&message=&checkClose=1&sessionTimeout=-1.0&guestmsgreq=false&logintype=2&username=$username&username1=$username&password=****&logout=Logout&saveinfo="
curl -b /tmp/24onlinecookie -c /tmp/24onlinecookie --data $post_data $url > /dev/null 2>&1
url="http://$srv_ip/24online/webpages/clientlogin.jsp?loginstatus=null&logoutstatus=true&liverequesttime=null&livemessage=null&url=null&isAccessDenied=false&fromlogout=null&sessionTimeout=-1.0"
curl -b /tmp/24onlinecookie -c /tmp/24onlinecookie $url > /dev/null 2>&1
get_tstamp
#wait for 5 seconds
tput setaf 6
echo "$tstamp: Please wait for 5 seconds for connection to stabilise"
tput sgr0
sleep 5
#test net connection
chk_connection
rm -f /tmp/24onlinecookie
end_rxbyte_count=$(printrxbytes);
end_txbyte_count=$(printtxbytes);
rx_count=$((end_rxbyte_count - start_rxbyte_count))
tx_count=$((end_txbyte_count - start_txbyte_count))
echo "Session Stats"
echo "Rx=" $rx_count "Bytes|"$((rx_count/1024)) "KB|"$((rx_count/1048756)) "MB|"$(($rx_count/1073741824)) "GB"
echo "Tx=" $rx_count "Bytes|"$((tx_count/1024)) "KB|"$((tx_count/1048756)) "MB|"$(($tx_count/1073741824)) "GB"
return $?
}
init_login()
{
get_tstamp
#attempt connection to cyberoam server to get the session cookie. save cookie locally
curl -c /tmp/24onlinecookie http://$srv_ip/24online/webpages/client.jsp > /dev/null 2>&1
session_id=$(sed -n -e 's/.*JSESSIONID\(.*\).*/\1/p' /tmp/24onlinecookie | sed -e 's/^[ \t]*//')
tput setaf 6
echo "$tstamp: Session id is $session_id"
tput sgr0
#attempt reverse session registration
url="http://$srv_ip/24online/webpages/clientlogin.jsp?loginstatus=null&logoutstatus=null&message=null&liverequesttime=null&livemessage=null&url=null&isAccessDenied=null&fromlogout=null&sessionTimeout=null"
referrer="http://$srv_ip/24online/webpages/client.jsp"
curl -b /tmp/24onlinecookie $url > /dev/null 2>&1
#attempt initial login
url="http://$srv_ip/24online/servlet/CyberoamHTTPClient"
post_data="mode=191&isAccessDenied=null&url=null&message=&checkClose=1&sessionTimeout=0.0&guestmsgreq=false&logintype=2&username=$username&password=$password&login=Login&saveinfo="
curl -b /tmp/24onlinecookie -c /tmp/24onlinecookie --data $post_data $url > /dev/null 2>&1
url="http://$srv_ip/24online/webpages/clientlogin.jsp?loginstatus=true&logoutstatus=null&liverequesttime=180&livemessage=null&url=null&isAccessDenied=null&fromlogout=null&sessionTimeout=-1"
curl -b /tmp/24onlinecookie -c /tmp/24onlinecookie $url > /dev/null 2>&1
url2="http://$srv_ip/24online/webpages/liverequest.jsp?isfirsttime=true"
curl --referrer $url -b /tmp/24onlinecookie -c /tmp/24onlinecookie $url2 > /dev/null 2>&1
get_tstamp
#wait for 5 seconds
tput setaf 6
echo "$tstamp: Please wait for 5 seconds for connection to stabilise"
tput sgr0
sleep 5
chk_connection
if [ $connected -eq 1 ]
then first_time=0
keep_alive_check_ts=$lastcheck_ts # to prevent premature keepalive packet being sent
fi
}
keep_alive()
{
get_tstamp
url="http://$srv_ip/24online/webpages/liverequest.jsp?isfirsttime=false"
curl -b /tmp/24onlinecookie -c /tmp/24onlinecookie $url > /dev/null 2>&1
url="http://$srv_ip/24online/servlet/CyberoamHTTPClient?mode=192&username=$username"
curl -b /tmp/24onlinecookie -c /tmp/24onlinecookie $url > /dev/null 2>&1
url="http://$srv_ip/24online/webpages/liverequest.jsp"
curl -b /tmp/24onlinecookie -c /tmp/24onlinecookie $url > /dev/null 2>&1
chk_connection
keep_alive_check_ts=$lastcheck_ts
}
chk_connection()
{
get_tstamp
#test net connection
if ping -c1 host.com > /dev/null
then
connected=1
if [ $first_time -eq 1 ] #display prompt only initially
then
tput setaf 2 && tput bold
echo "$tstamp: Yipee You are connected to Internet"
tput sgr0
fi
else
tput setaf 1 && tput bold
echo "$tstamp: You are not connected to Internet"
connected=0
tput sgr0
fi
lastcheck_ts=`date "+%s"` # set to current unix timestamp
}
get_tstamp()
{
tstamp=`date "+%Y-%m-%d %H:%M:%S"`
}
control_c()
# run if user hits control-c to exit the code
{
get_tstamp
tput setaf 3 && tput bold
echo -en "\n$tstamp: *** Ouch! Logging out ***\n"
tput sgr0
logout_session
exit $?
}
#byte counter
#source http://meinit.nl/shell-script-measure-network-throughput-linux-machines
printrxbytes(){
/sbin/ifconfig "$interface" | grep "RX bytes" | cut -d: -f2 | awk '{ print $1 }'
}
printtxbytes(){
/sbin/ifconfig "$interface" | grep "TX bytes" | cut -d: -f3 | awk '{ print $1 }'
}
#--------------------------------------------
start_rxbyte_count=$(printrxbytes);
start_txbyte_count=$(printtxbytes);
#--------------------------------------------
# trap keyboard control-c interrup for graceful exit
trap control_c SIGINT
#main loop
#check if connection is alive every 5s
#if yes do nothing
#if not connected then relogin
#if connection is alive for more than 3m then keep alive connection
while true
do
current_ts=`date "+%s"`
#check if connection is alive every 5s
if [ $((current_ts - lastcheck_ts)) -ge 5 ]
then chk_connection #lastcheck_ts is automatically set in this loop
fi
#if session is terminated by server then logout gracefully
if [ $connected -eq 0 -a $first_time -eq 0 ]
then
tput setaf 4
echo "$tstamp: It appears that the server terminated your session"
tput sgr0
logout_session
first_time=1
fi
#if not logged in initially then login
if [ $connected -eq 0 -a $first_time -eq 1 ]
then init_login
fi
#if connection is alive for more than 3m then keep alive connection
if [ $((current_ts - keep_alive_check_ts)) -ge 180 ]
then
tput setaf 6
echo -n "$tstamp: Refreshing Connection State"
keep_alive
if [ $connected -eq 1 ]
then
tput setaf 2 && tput bold
echo " [OK]"
tput sgr0
fi
fi
sleep 0.5 # sleep for half a second to prevent loop
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment