锐捷校园网认证脚本
# 锐捷校园网认证脚本
可以把这个脚本写入定时任务里,每分钟通过访问百度页面,判断是否掉线并重连。
import re
import requests
from datetime import datetime
# 请求头部
headers = {
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Mobile Safari/537.36',
}
def follow():
# 通过访问百度域名,在跳转页面js获取参数
fuck_url = 'http://baidu.com'
try:
response = requests.get(fuck_url, timeout=5, headers=headers)
if "百度一下" in response.text:
print("[-]:当前网络可用,无需再次认证!")
exit()
# 检查校园网解析到的网页,匹配网页js将要跳转的页面,即认证页面
js_redirect = re.search(r'self\.location\.href\s*=\s*["\'](.*?)["\']', response.text)
if js_redirect:
return js_redirect.group(1)
else:
print("[-]:follow()函数在定位校园网认证页面阶段,未能够获取js跳转参数......")
exit()
except requests.RequestException as e:
print(f"[!] request发起请求失败: {e}")
exit()
# 获取js跳转的IP地址
def get_ip():
url = follow()
# 使用正则提取URL中的IP
match = re.search(r'http://([a-zA-Z0-9.-]+)(:\d+)?/', url)
if match:
return match.group(1) # 返回IP或者域名部分
else:
print("[-]:get_ip()函数在解析校园网ip格式阶段,未能够获取到校园网ip......")
exit()
# 获取变量queryString
def get_query_string():
url = follow()
# 提取查询参数(? 后面的部分)
match = re.search(r'\?(.*)', url)
if match:
return match.group(1)
else:
print("[-]:get_query_string()函数在解析变量queryString阶段出错,未能够获取到变量queryString......")
exit()
# 测试
def test(data):
print(f"认证IP: {get_ip()}")
print(f"查询参数: {get_query_string()}")
print(f"认证地址:http://{get_ip()}/eportal/InterFace.do?method=login")
print(f"请求数据: {data}")
# 检查账号密码是否为空
if data['userId'] == '' or data['password'] == '':
print("[-]:账号密码未填写")
exit()
def auto_link_wifi(url, data):
# 发送POST请求
resp = requests.post(url, headers=headers, data=data)
# 判断认证是否成功
if resp.status_code == 200:
if "success" in resp.text:
res_message = "认证成功,成功连接校园网"
elif "fail" in resp.text:
res_message = "认证失败,请检查账号密码是否正确"
print(res_message)
else:
res_message = "脚本可能并不适配当前网络环境"
# 记录日志
with open("./auto-link-wifi.log", "a") as file:
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
file.write(f"[{now}] {res_message}\n")
# 校园网认证界面
url = f"http://{get_ip()}/eportal/InterFace.do?method=login"
# 认证提交的数据
# 注意:账号密码必填
data = {
# 校园网账号
'userId': '',
# 校园网明文密码
'password': '',
'service': '',
'queryString': get_query_string(),
'operatorPwd': '',
'operatorUserId': '',
'validcode': '',
'passwordEncrypt': 'false',
}
# 测试
test(data)
# 自动连接
auto_link_wifi(url,data)
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
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
编辑 (opens new window)
最后一次更新于: 2025/03/11, 17:53:39