Created
April 24, 2019 13:12
-
-
Save WuSiYu/edb10f958cdfecc28688f02e0ec83deb to your computer and use it in GitHub Desktop.
北京工业大学校园网自动登录SHELL脚本
This file contains 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
#!/bin/sh | |
# Copyright (c) 2019 SiYu Wu <wu.siyu@hotmail.com> | |
# BJUT gateway auto login script v0.1 | |
# Usage: bjut-autologin.sh <user> <password> | |
# set LOGIN_IPV6=1 to enable ipv6 login, it will use one extra session on WiFi. | |
LOGIN_IPV6=${LOGIN_IPV6:-0} | |
if [ $# -ne 2 ] && !([ $# -eq 1 ] && ([[ $1 == '-l' ]] || [[ $1 == '--logout' ]]) ); then | |
echo "Usage: $0 <user> <password>" | |
echo " $0 -l, --logout" | |
echo " set LOGIN_IPV6=1 to enable ipv6 login, it will use one extra session on WiFi." | |
exit 1 | |
fi | |
if [ $# -eq 1 ] && ([[ $1 == '-l' ]] || [[ $1 == '--logout' ]]); then | |
res=`curl -m 2 -s https://lgn.bjut.edu.cn/F.htm` | |
if [ `echo $res | grep -c 'Logout Error(no webmode)'` -eq 1 ]; then | |
echo 'WiFi connection detected' | |
res=`curl -m 2 -s https://wlgn.bjut.edu.cn/F.htm` | |
if [ `echo $res | grep -c 'Msg=14'` -eq 1 ]; then | |
echo '==> WiFi logout successful' | |
else | |
echo 'Error: WiFi logout failed!' | |
fi | |
elif [ `echo $res | grep -c 'Msg=14'` -eq 1 ]; then | |
echo '==> Wired logout successful' | |
else | |
echo 'Error: Wired logout failed!' | |
fi | |
exit 0 | |
fi | |
lgn_code=`curl -m 2 -s -o /dev/null -w "%{http_code}" http://lgn.bjut.edu.cn` | |
if [ $lgn_code -eq 302 ]; then | |
echo 'WiFi connection detected' | |
res=`curl -m 2 -s --data "DDDDD=$1&upass=$2&6MKKey=%B5%C7%C2%BC+Login" https://wlgn.bjut.edu.cn` | |
if [ `echo $res | grep -c 'successfully logged in'` -eq 1 ]; then | |
echo '==> WiFi ipv4 login successful' | |
else | |
echo 'Error: WiFi ipv4 login failed!' | |
fi | |
if [ $LOGIN_IPV6 -ne 0 ]; then | |
res=`curl -m 2 -s --data "DDDDD=$1&upass=$2&v46s=2&v6ip=&f4serip=172.30.201.2&0MKKey=" https://lgn6.bjut.edu.cn/` | |
if [ `echo $res | grep -c 'successfully logged in'` -eq 1 ]; then | |
echo '==> WiFi ipv6 login successful' | |
else | |
echo 'Error: WiFi ipv6 login failed!' | |
fi | |
fi | |
elif [ $lgn_code -eq 200 ]; then | |
lgn_response=`curl -m 2 -s http://lgn.bjut.edu.cn` | |
if [ `echo $lgn_response | grep -c Balance` -eq 1 ]; then | |
echo "Error: Already logged in" | |
exit 2 | |
else | |
echo 'Wired connection detected' | |
if [ $LOGIN_IPV6 -ne 0 ]; then | |
res=`curl -m 2 -s --data "DDDDD=$1&upass=$2&v46s=0&v6ip=&f4serip=172.30.201.2&0MKKey=" https://lgn6.bjut.edu.cn/V6?https://lgn.bjut.edu.cn` | |
v6ip=${res:419:39} # TODO: This implementation is not aesthetically pleasing due to compatibility with small systems without `sed` command. | |
res=`curl -m 2 -s --data "DDDDD=$1&upass=$2&0MKKey=Login&v6ip=$v6ip" https://lgn.bjut.edu.cn/` | |
if [ `echo $res | grep -c 'successfully logged in'` -eq 1 ]; then | |
echo '==> Wired ipv4+ipv6 login successful' | |
else | |
echo 'Error: Wired ipv4+ipv6 login failed!' | |
fi | |
else | |
res=`curl -m 2 -s --data "DDDDD=$1&upass=$2&v46s=1&v6ip=&f4serip=172.30.201.2&0MKKey=" https://lgn.bjut.edu.cn` | |
if [ `echo $res | grep -c 'successfully logged in'` -eq 1 ]; then | |
echo '==> Wired ipv4 login successful' | |
else | |
echo 'Error: Wired ipv4 login failed!' | |
fi | |
fi | |
fi | |
else | |
echo "Error: no connection to gateway" | |
exit 2 | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment