用终端登录校园网

昨晚和队友们聚会,打车时想到了自己之前遇到的一个待解决问题(用终端登录校园网),就提了出来。一个队友说可以写个脚本,另一个队友说他以前写过,然后我就直接拿来用了。login-USTB-campus-network

代码如下:(注释得非常到位,让我看了觉得很惭愧~)

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
# coding=utf8
"""
The script is used to log in USTB campus network
Modify username and password to yours
Put the script in computer startup items
Then computer will log in campus network automatically when startup
Python version:2.7.5
"""


import httplib
import urllib
import subprocess
import re

"""
This function will return computer's local temporary ipv6 address if it exists,
otherwise return None.
Every computer has two ipv6 addresses, a ipv6 address and a temporary ipv6 address.
We use the temporary ipv6 address.
"""

def get_local_tmp_v6ip():
get_ipv6_process = subprocess.Popen("ipconfig", stdout = subprocess.PIPE)
output = (get_ipv6_process.stdout.read())
ipv6_pattern='(([a-f0-9]{1,4}:){7}[a-f0-9]{1,4})'
m = re.findall(ipv6_pattern, str(output))
if m is not None:
#m is a list of tuples
#m[1] is the second tuple
#m[1][0] is the temporary ipv6 address
return m[1][0]
else:
return None

#modify username and password to yours
username='your username'
password='your password'
savePWD='on'

headers = {"Connection": "keep-alive",
"Cookie": "myusername=%s; username=%s; smartdot=%s; pwd=%s" % (username, username, password, password),
"Content-Type": "application/x-www-form-urlencoded"}

body = urllib.urlencode({'DDDDD':username,
'upass':password,
'0MKKey':'123456789',
'v6ip':get_local_tmp_v6ip(),
'savePWD':savePWD})

conn = httplib.HTTPConnection("202.204.48.66")
conn.request("POST", url="/", body = body, headers = headers)
response = conn.getresponse()
if response.status == 200:
print response.status, response.reason
print response.msg
#conn.close()

#get your network information
#print response.read()

处理过程就是往校园网登陆网址(202.204.48.66)提交POST请求;提交请求时附加自己的校园网账户信息,强迫症特意查看了一下登录页面的源代码,参数一致。代码是用Python2.7写的,然后自己又修改出了一个Python3.5的版本:

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
# coding=utf8
"""
The script is used to log in USTB campus network
Modify username and password to yours
Put the script in computer startup items
Then computer will log in campus network automatically when startup
Python version:3.5.1
"""


import httplib2
import urllib
import subprocess
import re

"""
This function will return computer's local temporary ipv6 address if it exists,
otherwise return None.
Every computer has two ipv6 addresses, a ipv6 address and a temporary ipv6 address.
We use the temporary ipv6 address.
"""



def get_local_tmp_v6ip():
get_ipv6_process = subprocess.Popen("ifconfig", stdout=subprocess.PIPE)
output = (get_ipv6_process.stdout.read())
ipv6_pattern = '(([a-f0-9]{1,4}:){7}[a-f0-9]{1,4})'
m = re.findall(ipv6_pattern, str(output))
if m is not None:
# m is a list of tuples
# m[1] is the second tuple
# m[1][0] is the temporary ipv6 address
return m[1][0]
else:
return None


# modify username and password to yours
username='your username'
password='your password'
savePWD = 'on'

headers = {"Connection": "keep-alive",
"Cookie": "myusername=%s; username=%s; smartdot=%s; pwd=%s" % (username, username, password, password),
"Content-Type": "application/x-www-form-urlencoded"}

body = urllib.parse.urlencode({'DDDDD': username,
'upass': password,
'0MKKey': '123456789',
'v6ip': get_local_tmp_v6ip(),
'savePWD': savePWD})

http = httplib2.Http()
response, content = http.request("http://202.204.48.66", "POST", body=body, headers=headers)
if response.status == 200:
print(response)
print(content)

然后又想到,这种事情说得简单点,就是一个网络请求,那直接用urllib来做就是了,于是有了下面这个:

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
# coding=utf8
"""
The script is used to log in USTB campus network
Modify username and password to yours
Put the script in computer startup items
Then computer will log in campus network automatically when startup
Python version:3.5.1
"""


from urllib import request, parse
import subprocess, re


def get_local_tmp_v6ip():
get_ipv6_process = subprocess.Popen("ifconfig", stdout=subprocess.PIPE)
output = (get_ipv6_process.stdout.read())
ipv6_pattern = '(([a-f0-9]{1,4}:){7}[a-f0-9]{1,4})'
m = re.findall(ipv6_pattern, str(output))
if m is not None:
# m is a list of tuples
# m[1] is the second tuple
# m[1][0] is the temporary ipv6 address
return m[1][0]
else:
return None


# modify username and password to yours
username='your username'
password='your password'
savePWD = 'on'

body = parse.urlencode({'DDDDD': username,
'upass': password,
'0MKKey': '123456789',
'v6ip': get_local_tmp_v6ip(),
'savePWD': savePWD})

req = request.Request("http://202.204.48.66")
req.add_header('Cookie', 'myusername=%s; username=%s; smartdot=%s; pwd=%s' % (username, username, password, password))

print('Logging in Campus Network……')
with request.urlopen(req, data=body.encode('utf-8')) as f:
print('Status:', f.status, f.reason)
for k, v in f.getheaders():
print('%s: %s' % (k, v))
print('Content:', f.read())

至于为什么用urllib而去掉了前面得httplib,我觉得可以用Stack Overflow上面的一段话来解释:

urllib (particularly urllib2) handles many things by default or has appropriate libs to do so. For example, urllib2 will follow redirects automatically and you can use cookiejar to handle login scripts. These are all things you’d have to code yourself if you were using httplib.

八宝粥 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!