Skip to content

Instantly share code, notes, and snippets.

@shangjiyu
Created October 10, 2015 15:05
Show Gist options
  • Save shangjiyu/ef500b5729f4c4db97f4 to your computer and use it in GitHub Desktop.
Save shangjiyu/ef500b5729f4c4db97f4 to your computer and use it in GitHub Desktop.
cmcc-edu login/logout shell script fork from http://www.openwrt.org.cn/bbs/thread-12494-1-1.html
#!/bin/sh
# cmcc auto login
# by Huson
# 2012-12-25
#export PATH=.:$PATH
#cd /root
# set var 18000=5h
userName="137********"
userPwd="******"
start_time="08"
off_time="24"
relogin_time="18000"
ping_test="114.114.114.114"
open_url="http://www.baidu.com/"
# function
get_data()
{
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.8 Safari/537.36"
if [ "$1" = "in" ]; then
wget -O login_pre.html --save-cookies cmcc_cookies.txt --keep-session-cookies -U "${user_agent}" ${open_url}
action_url=$(cat login_pre.html|grep "Wlan_Login" |awk -F '"' '{if($2!="") print $2}' |head -1)
httpBase=$(cat login_pre.html |grep "httpBase" |awk -F '"' '{if($2!="") print $2}' |head -1)
ctxPath=$(cat login_pre.html |grep "ctxPath" |awk -F '"' '{if($2!="") print $2}' |head -1)
page_name="/portalLoginRedirect.wlan"
redirect_url=${httpBase}${ctxPath}${page_name}
wlanAcName=$(cat login_pre.html|grep "wlanAcName" |awk -F '"' '{if($8!="") print $8}' |head -1)
wlanUserIp=$(cat login_pre.html|grep "wlanUserIp" |awk -F '"' '{if($8!="") print $8}' |head -1)
wlanAcIp=$(cat login_pre.html|grep "wlanAcIp" |awk -F '"' '{if($8!="") print $8}' |head -1)
ssid=$(cat login_pre.html|grep "ssid" |awk -F '"' '{if($8!="") print $8}' |head -1)
URL="wlanAcName=${wlanAcName}&wlanUserIp=${wlanUserIp}&ssid=${ssid}&wlanAcIp=${wlanAcIp}&userName=${userName}&userPwd=${userPwd}"
echo $URL
fi
if [ "$1" = "mid" ]; then
action_url=$(cat login_mid.html|grep "action" |awk -F '"' '{if($4!="") print $4}' |head -1)
validperiod=$(cat login_mid.html |grep "validperiod" |awk -F '"' '{if($2!="") print $2}' |head -1)
isLocalUser=$(cat login_mid.html |grep "isLocalUser" |awk -F '"' '{if($2!="") print $2}' |head -1)
wlanAcName=$(cat login_mid.html|grep "wlanAcName" |awk -F '"' '{if($8!="") print $8}' |head -1)
wlanUserIp=$(cat login_mid.html|grep "wlanUserIp" |awk -F '"' '{if($8!="") print $8}' |head -1)
wlanAcIp=$(cat login_mid.html|grep "wlanAcIp" |awk -F '"' '{if($8!="") print $8}' |head -1)
ssid=$(cat login_mid.html|grep "ssid" |awk -F '"' '{if($8!="") print $8}' |head -1)
encryUser=$(cat login_mid.html|grep "encryUser" |awk -F '"' '{if($8!="") print $8}' |head -1)
passType=$(cat login_mid.html|grep "passType" |awk -F '"' '{if($8!="") print $8}' |head -1)
freePkgTotal=$(cat login_mid.html|grep "freePkgTotal" |awk -F '"' '{if($8!="") print $8}' |head -1)
logonsessid=$(cat login_mid.html|grep "logonsessid" |awk -F '"' '{if($8!="") print $8}' |head -1)
userType=$(cat login_mid.html|grep "userType" |awk -F '"' '{if($8!="") print $8}' |head -1)
wlanFees=$(cat login_mid.html|grep "wlanFees" |awk -F '"' '{if($8!="") print $8}' |head -1)
usedFree=$(cat login_mid.html|grep "usedFree" |awk -F '"' '{if($8!="") print $8}' |head -1)
booktime=$(cat login_mid.html|grep "booktime" |awk -F '"' '{if($8!="") print $8}' |head -1)
AUTO_LOGIN=$(cat login_mid.html|grep "AUTO_LOGIN" |awk -F '"' '{if($8!="") print $8}' |head -1)
productName=$(cat login_mid.html|grep "productName" |awk -F '"' '{if($8!="") print $8}' |head -1)
feesSum=$(cat login_mid.html|grep "feesSum" |awk -F '"' '{if($8!="") print $8}' |head -1)
URL="wlanAcName=${wlanAcName}&wlanUserIp=${wlanUserIp}&ssid=${ssid}&wlanAcIp=${wlanAcIp}&userName=${userName}&encryUser=${encryUser}&validperiod=${validperiod}&isLocalUser=${isLocalUser}&passType=${passType}&freePkgTotal=${freePkgTotal}&logonsessid=${logonsessid}&userType=${userType}&wlanFees=${wlanFees}&usedFree=${usedFree}&booktime=${booktime}&AUTO_LOGIN=${AUTO_LOGIN}&productName=${productName}&feesSum=${feesSum}"
echo $URL
fi
if [ "$1" = "out" ]; then
httpBase=$(cat login_res.html |grep "httpBase" |awk -F '"' '{if($2!="") print $2}' |head -1)
ctxPath=$(cat login_res.html |grep "ctxPath" |awk -F '"' '{if($2!="") print $2}' |head -1)
actionPath="/portalLogout.wlan?isCloseWindow=N&"
logout_url=$httpBase$ctxPath$actionPath
wlanAcName=$(cat login_res.html|grep "wlanAcName" |awk -F '"' '{if($8!="") print $8}' |head -1)
wlanUserIp=$(cat login_res.html|grep "wlanUserIp" |awk -F '"' '{if($8!="") print $8}' |head -1)
wlanAcIp=$(cat login_res.html|grep "wlanAcIp" |awk -F '"' '{if($8!="") print $8}' |head -1)
ssid=$(cat login_res.html|grep "ssid" |awk -F '"' '{if($8!="") print $8}' |head -1)
userName=$(cat login_res.html|grep "userName" |awk -F '"' '{if($8!="") print $8}' |head -1)
encryUser=$(cat login_res.html|grep "encryUser" |awk -F '"' '{if($8!="") print $8}' |head -1)
passType=$(cat login_res.html|grep "passType" |awk -F '"' '{if($8!="") print $8}' |head -1)
isLocalUser=$(cat login_res.html|grep "isLocalUser" |awk -F '"' '{if($8!="") print $8}' |head -1)
URL="wlanAcName=${wlanAcName}&wlanUserIp=${wlanUserIp}&wlanAcIp=${wlanAcIp}&ssid=${ssid}&userName=${userName}&encryUser=${encryUser}&passType=${passType}&isLocalUser=${isLocalUser}"
echo $URL
fi
}
log_in_mid(){
get_data mid
wget --post-data ${URL} -O login_res.html --load-cookies cmcc_cookies.txt --keep-session-cookies -U "${user_agent}" --referer= ${action_url} ${redirect_url}
}
log_in()
{
get_data in
wget --post-data ${URL} -O login_mid.html --load-cookies cmcc_cookies.txt --keep-session-cookies -U "${user_agent}" --referer= ${action_url} ${redirect_url}
log_in_mid
grep "per_name login_cg" login_res.html|head -1
date "+%Y-%m-%d %H:%M:%S" > in_time.txt
# export login_time=`date "+%Y-%m-%d %H:%M:%S"`
}
log_out()
{
pings=`ping -c 3 ${ping_test} |grep 'from' |wc -l`
if [ ${pings} -le 1 ]; then rm login_pre.html; rm login_mid.html; rm login_res.html; rm cmcc_cookies.txt; rm in_time.txt; exit; fi
if [ -e "logout_res.html" ]; then
rm logout_res.html
fi
get_data out
wget --post-data ${URL} -O logout_res.html --load-cookies cmcc_cookies.txt -U "${user_agent}" ${logout_url}
rm login_pre.html; rm login_res.html; rm cmcc_cookies.txt; rm in_time.txt
grep "alert" logout_res.html|head -1
}
if [ "$1" = "logout" ]; then
log_out
exit
fi
if [ "$1" = "login" ]; then
log_in
exit
fi
if [ "$1" = "login_mid" ]; then
log_in_mid
exit
fi
## off time
#if [ `date "+%H"` -ge ${off_time} ]; then
# log_out
# exit
#fi
#if [ `date "+%H"` -lt ${start_time} ]; then exit; fi
## relogin
#if [ -e in_time.txt ]; then
# login_time=$(cat in_time.txt|head -1)
# now_time=`date "+%Y-%m-%d %H:%M:%S"`
# now_t=`date -d "$now_time" +%s`
# login_t=`date -d "$login_time" +%s`
# let over_time=${now_t}-${login_t}
# if [ "${over_time}" -ge "${relogin_time}" ]; then
# log_out
# log_in
# exit
# fi
#else
# log_in
# exit
#fi
## normal connection, exit
#pings=`ping -c 3 ${ping_test} |grep 'from' |wc -l`
#if [ "${pings}" -gt "1" ]; then exit; fi
##log_in
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment