-
Notifications
You must be signed in to change notification settings - Fork 11
/
netTHU
executable file
·102 lines (93 loc) · 2.97 KB
/
netTHU
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
#author: guzhaoyuan
if [ $# -eq 0 ]; then
echo "Missing options!"
echo "(run $0 -h for help)"
echo ""
exit 0
fi
function login() {
local username=$1
local passmd5=$2
result=$(curl -sL net.tsinghua.edu.cn/do_login.php --data "action=login&username="$username"&password={MD5_HEX}"$passmd5"&ac_id=1")
res=`echo $result |awk '{print $3}'`
if [ $res != "successful." ]; then
error=`echo $result | awk '{print $1}'`
if [ $error = "E2532:" ];then
echo "login too frequently, wait for 10 seconds"
elif [ $error = "E2553:" ];then
echo "wrong account info record"
echo "please use -r to remove account"
else
echo $result
fi
else
echo $result
fi
}
while getopts "odhrl" OPTION; do
case $OPTION in
o)
if [ ! -d ~/.TsinghuaNet ]; then
mkdir ~/.TsinghuaNet
fi
if [ -a ~/.TsinghuaNet/netTHUservice ]; then
username=`awk 'NR==1 {print $2}' ~/.TsinghuaNet/netTHUservice`
passmd5=`awk 'NR==2 {print $2}' ~/.TsinghuaNet/netTHUservice`
login $username $passmd5
else
echo "first use please input username and password"
echo -e "username:\c"
read username
echo "username "$username > ~/.TsinghuaNet/netTHUservice
echo -e "password:\c"
read -s password
echo ""
if [ $(uname)=='Darwin' ]; then
passmd5=$(echo -n $password | md5)
else
passmd5=$(echo -n $password | md5sum | cut -d' ' -f1)
fi
echo "password "$passmd5 >> ~/.TsinghuaNet/netTHUservice
echo "password remembered."
login $username $passmd5
fi
exit 0
;;
d)
curl -L net.tsinghua.edu.cn/do_login.php --data "action=logout"
echo ""
exit 0
;;
h)
echo "Usage:"
echo "netTHU -h"
echo ""
echo " -o to login Tsinghua net"
echo " -d to logout Tsinghua net"
echo " -l to list net.tsinghua data usage"
echo " -r to remove account info"
echo " -h help (this output)"
exit 0
;;
r)
rm ~/.TsinghuaNet/netTHUservice
exit 0
;;
l)
raw_data=`curl -sL net.tsinghua.edu.cn/rad_user_info.php | cut -d',' -f7`
if [ -n "$raw_data" ];then
echo ""
echo "Current Data Usage(G): "$((raw_data*1/10000000))| sed 's/..$/.&/'
echo ""
else
echo "Please login first."
fi
exit 0
;;
*)
echo "illegal option"
exit 0
;;
esac
done