Naitong Yu

思索与前行

Linux下登录新版TUNet的bash脚本

今晚闲来无事,便写了一个登录新版TUNet的脚本,因为是一边翻书一边写的,所以非常挫,不过应该能用。

写这个最初目的是做嵌入式的大作业的,因为TUNet更新了,所以不能做以前的那个mytunet的交叉编译了。。。

脚本在Openwrt上测试通过。

把脚本中的your_username替换成info的用户名,your_password_md5替换成你的密码的md5,可以用echo -n your_password | md5sum来得到。另外脚本需要curl,Ubuntu用户可以用sudo apt-get install curl来安装。

#!/bin/sh
# your info's username
uname=your_username
# your info's password's md5sum
pass=your_password_md5
####################################################
do_login() {
	login_data='username='$uname'&password='$pass'&drop=0&type=1&n=100'
	check_data='action=check_online'

	# check whether already online
	con=`curl -d $check_data -s http://net.tsinghua.edu.cn/cgi-bin/do_login`

	if [ -z $con ]; then
		# start login
		res=`curl -d $login_data -s http://net.tsinghua.edu.cn/cgi-bin/do_login`

		#handle result
		pe=`echo $res | grep error`
		if [ -z $pe ]; then
			echo "Login Success!"	
		else
			echo $pe
			exit 0
		fi

		# display flux infomation
		flux=`echo $res | awk -F ',' '{print $3}'`
		a=$(($flux/1000000000))
		b=$((($flux%1000000000)/100000000))
		c=$((($flux%100000000)/10000000))
		echo "Used Flux: "$a"."$b$c"G."	

	else
		echo "Already Online!"

		# display flux information and online time
		flux=`echo $con | awk -F ',' '{print $3}'`
		time=`echo $con | awk -F ',' '{print $5}'`
		a=$(($flux/1000000000)) 
		b=$((($flux%1000000000)/100000000))
		c=$((($flux%100000000)/10000000))
		h=$(($time/3600))
		m=$(($(($time%3600))/60))
		s=$(($(($time%3600))%60))
		echo "Used Flux: "$a"."$b$c"G, Online Time: "$h":"$m":"$s"."
	fi
}

do_logout() {
	# start logout
	res=`curl -s http://net.tsinghua.edu.cn/cgi-bin/do_logout`

	#handle result
	if [ "$res" == "logout_ok" ]; then
		echo "Logout Success!"
	elif [ "$res" == "not_online_error" ]; then
		echo "You're not Online!"
	else
		echo "Operation Failed!"
	fi
}

#############################################################
if [ "$1" == "login" ]; then
	do_login
elif [ "$1" == "logout" ]; then
	do_logout
else
	echo "Usage: "$0" {login|logout}"
fi

此脚本理论上也适用于Mac OS X以及FreeBSD等*NIX系统。