This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipgw.sh
executable file
·239 lines (226 loc) · 6.92 KB
/
ipgw.sh
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/bash
#########################################################################
# File Name: ipgw.sh
# Author: Li Jiahao
# ###############
# mail: [email protected]
# Created Time: Sat 21 May 2016 10:50:21 AM CST
#########################################################################
usage()
{
echo "Usage: `basename $0` [-c|-d] [-q] [-f] [-m] [-u <username>] [-p <password>]"
echo "Options:"
echo -e "-c, --connect\tConnect NEU IP Gateway"
echo -e "-d, --disconnect\tDisconnect NEU IP Gateway"
echo -e "-f, --force\tPerform force connection"
echo -e "-m, --mobile\tConnect as mobile device"
echo -e "-q, --query\tQuery use of flow, balance, etc"
echo -e "-u, --username VALUE\tSpecify username"
echo -e "-p, --password VALUE\tSpecify password"
}
invalidargs()
{
echo "Invaild Options!"
echo ""
usage
exit 1
}
requireuinfo()
{
if [ ! -n "$USER_NAME" ];
then
echo "请输入校园网账号:"
read USER_NAME
fi
if [ ! -n "$USER_PASS" ];
then
echo "请输入校园网密码:"
stty -echo
read USER_PASS
stty echo
fi
}
disconnect()
{
requireuinfo
if [ ! -x "$(command -v curl)" ]; then
if [ ! -x "$(command -v wget)" ]; then
echo "Error: Neither curl nor wget is installed"
else
LOGOUT=`wget -q --post-data="action=logout&ac_id=1&user_ip=&nas_ip=&user_mac=&url=&username=$USER_NAME&password=$USER_PASS&save_me=0" "https://ipgw.neu.edu.cn/srun_portal_pc.php?ac_id=1&" -O - 2>&1`
fi
else
LOGOUT=`curl -s -k -d "action=logout&ac_id=1&user_ip=&nas_ip=&user_mac=&url=&username=$USER_NAME&password=$USER_PASS&save_me=0" "https://ipgw.neu.edu.cn/srun_portal_pc.php?ac_id=1&" 2>&1`
fi
if [[ "$LOGOUT" =~ "网络已断开" ]];
then
echo "断开连接成功!"
else
echo "断开连接失败!"
TEMP=${LOGOUT#*<td height=\"40\" style=\"font-weight:bold;color:orange;\">}
echo ${TEMP%%</td>*}
fi
}
connectasphone()
{
requireuinfo
if [ ! -x "$(command -v curl)" ]; then
if [ ! -x "$(command -v wget)" ]; then
echo "Error: Neither curl nor wget is installed"
else
LOGIN=`wget --header="Content-type: application/x-www-form-urlencoded" --header="User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A366 Safari/600.1.4" -q --post-data="action=login&ac_id=1&user_ip=&nas_ip=&user_mac=&url=&username=$USER_NAME&password=$USER_PASS&save_me=0" "https://ipgw.neu.edu.cn/srun_portal_pc.php?ac_id=1&" -O - 2>&1`
fi
else
LOGIN=`curl -k -H "Content-type: application/x-www-form-urlencoded" -A "Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A366 Safari/600.1.4" -s -d "action=login&ac_id=1&user_ip=&nas_ip=&user_mac=&url=&username=$USER_NAME&password=$USER_PASS&save_me=0" "https://ipgw.neu.edu.cn/srun_portal_pc.php?ac_id=1&" 2>&1`
fi
if [[ "$LOGIN" =~ "网络已连接" ]];
then
echo "网络连接成功!"
else
echo "网络连接失败!"
TEMP=${LOGIN#*<p>}
echo ${TEMP%%</p>*}
fi
query
}
connect()
{
requireuinfo
if [ ! -x "$(command -v curl)" ]; then
if [ ! -x "$(command -v wget)" ]; then
echo "Error: Neither curl nor wget is installed"
else
LOGIN=`wget --header="Content-type: application/x-www-form-urlencoded" --header="User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36" -q --post-data="action=login&ac_id=1&user_ip=&nas_ip=&user_mac=&url=&username=$USER_NAME&password=$USER_PASS&save_me=0" "https://ipgw.neu.edu.cn/srun_portal_pc.php?ac_id=1&" -O - 2>&1`
fi
else
LOGIN=`curl -k -H "Content-type: application/x-www-form-urlencoded" -A "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36" -s -d "action=login&ac_id=1&user_ip=&nas_ip=&user_mac=&url=&username=$USER_NAME&password=$USER_PASS&save_me=0" "https://ipgw.neu.edu.cn/srun_portal_pc.php?ac_id=1&" 2>&1`
fi
if [[ "$LOGIN" =~ "网络已连接" ]];
then
echo "网络连接成功!"
else
echo "网络连接失败!"
TEMP=${LOGIN#*<p>}
echo ${TEMP%%</p>*}
fi
query
}
query()
{
if [ ! -x "$(command -v curl)" ]; then
if [ ! -x "$(command -v wget)" ]; then
echo "Error: Neither curl nor wget is installed"
else
INFO=`wget -q --post-data="action=get_online_info&key=$k" "https://ipgw.neu.edu.cn/include/auth_action.php?k=$k" -O - 2>&1`
fi
else
INFO=`curl -k -s -d "action=get_online_info&key=$k" "https://ipgw.neu.edu.cn/include/auth_action.php?k=$k" 2>&1`
fi
#echo $k;
#echo $INFO;
DATA=${INFO%%,*}
DATAG=$[$DATA/(1024*1024*1024)]
DATAM=$[$DATA%(1024*1024*1024)/(1024*1024)]
if [ $DATAG != 0 ];
then
echo "已用流量:$DATAG GiB $DATAM MiB"
else
echo "已用流量:$DATAM MiB"
fi
TEMP=${INFO#*,}
TIME=${TEMP%%,*}
TIMEH=$[$TIME/(60*60)]
TIMEM=$[$TIME%(60*60)/(60)]
TIMES=$[$TIME%60]
echo "已用时长:$TIMEH:$TIMEM:$TIMES"
TEMP=${TEMP#*,}
BALANCE=${TEMP%%,*}
echo "账户余额:$BALANCE"
IP=${INFO##*,}
echo "IP地址:$IP"
}
opt=""
SOURCE="$0"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
if [ -f "$DIR/user.cfg" ]; then
source $DIR/user.cfg
elif [ -f "/home/`whoami`/.neuipgw/user.cfg" ]; then
source /home/`whoami`/.neuipgw/user.cfg
else
echo "No config file found for current user,please specify username! See ipgw --help"
echo -e "You can generate a config file by running the installer with command\n\n sh $DIR/install.sh\n"
fi
temp=$(getopt -q -o qcdfmu:p:h --long connect,disconnect,force,mobile,query,username:,password:,help -- "$@")
if [ $? != 0 ]
then
invalidargs
fi
eval set -- "$temp"
while [ -n "$1" ]
do
case $1 in
-c|--connect) OPT_CONNECT=true
OPT_QUERY=false
shift
;;
-d|--disconnect) OPT_DISCONNECT=true
shift
;;
-f|--force) OPT_FORCE=true
shift
;;
-m|--mobile) OPT_MOBILE=true
shift
;;
-q|--query) OPT_QUERY=true
OPT_DISCONNECT=false
shift
;;
-u|--username) USER_NAME=$2
unset USER_PASS
shift 2
;;
-p|--password) USER_PASS=$2
shift 2
;;
-h|--help) usage
exit 0
;;
--)
shift
;;
*) usage
;;
esac
done
k=$RANDOM
#p=$(($RANDOM % 4 + 801))
if [[ $OPT_CONNECT = true ]] || [ ! -n "$OPT_DISCONNECT" ];
then
if [[ $OPT_FORCE = true ]];
then
disconnect
fi
if [[ $OPT_MOBILE = true ]];
then
connectasphone
else
connect
fi
elif [[ $OPT_DISCONNECT = true ]];
then
disconnect
fi
if [[ $OPT_QUERY = true ]];
then
query
fi
#echo $p
#echo $LOGOUT
#echo $LOGIN
sleep 2s