3

北京工业大学校园网自动登录shell脚本

 3 years ago
source link: https://wusiyu.me/bjut-shell-auto-login/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

北京工业大学校园网自动登录shell脚本

运动会没课,正好有时间,用shell脚本重写了一下之前的python自动登录脚本,这次写死了一些在北工大校园网内不会变的值,比原来完全python爬虫模拟的方式大幅简化,并且能够自动判断WiFi和有线网,同样支持IPv6登录(默认不启用)。

github地址:https://gist.github.com/WuSiYu/edb10f958cdfecc28688f02e0ec83deb

使用很简单,在后面加入用户名和密码即可,有线无线会自动判断。默认下,有线以“ipv4认证”登录,WiFi正常登录:

./bjut-autologin.sh <user> <password>

设置LOGIN_IPV6环境变量后,将启用ipv6登录,此时有线以“ipv4与ipv6统一认证”,WiFi下会在正常登录后再到lgn6.bjut.edu.cn进行“ipv6认证”,在WiFi下会导致多占用一个session,一共占用两个session(都9102年了还限制2 session登录的学校是屑)。而有线因为有“统一认证”,所以还是一个session:

LOGIN_IPV6=1 ./bjut-autologin.sh <user> <password>

同时本脚本还有“一键登出”的功能,在脚本后面加上-l--logout即可,有线无线同样是自动判断:

./bjut-autologin.sh -l

本脚本仅依赖curl,在openwrt上可能需要手动安装一下。不过就算用自带的wget,还是得手动装https的支持(在某工大不隔离的大内网下用http的理智程度大概并不高),所以综合来讲还是curl比较划算。

runing on openwrt

#!/bin/sh

# Copyright (c) 2019 SiYu Wu <[email protected]>
# 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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK