前言
贵州大学的校园网认证是向服务器发送一个POST请求,本教程使用Python代码实现这一过程,请注意本教程仅适用于贵州大学的校园网自动认证。
准备工作
1.Python工作环境
2.安装Python库(requests,re,socket,uuid)
安装命令
pip install 库名
宿舍端(GuizhouUniversity)
使用下面的代码需要将写有注释的地方更改为自己的信息
完整版代码包含了贵州大学校园网认证的所有过程,但含有较多冗余代码,其中wlanacip项在认证阶段并不是必须的,若你不知道自己的wlanacip,则可将这行代码删除。
精简版代码是在完整版代码基础上进行的精简,也可以完成自动认证过程,建议使用此代码。
完整版代码
import requests
import re
import socket
import uuid
# 重定向,本程序只能在宿舍楼使用,因为图书馆ac接入点ip不一致
url = "http://www.msftconnecttest.com/redirect"
session = requests.Session()
response = session.get(url, allow_redirects=True)
response.encoding = 'GBK'
text1 = response.text
pattern = re.search('\"(.*?)\"', text1)
# result = pattern.match(text1)
url2 = str(pattern.group(0))
url2 = url2.replace("\"", "", 2)
response1 = session.get(url2, allow_redirects=True)
response1.encoding = 'GBK'
text2 = response1.text
pattern1 = re.search('\"(.*?)\"', text2)
url3 = str(pattern1.group(0))
url3 = url3.replace("\"", "", 2)
response2 = session.get(url3, allow_redirects=True)
response2.encoding = 'GBK'
text3 = response2.text
for cookie in response2.cookies:
print(cookie)
# 后端请求
url4 = "http://172.16.4.31/questionnaireAction.do"
data = {
"act": "check",
"user_id": "#####" # 修改为自己的用户名
}
header = {
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
"Connection": "keep-alive",
"Content-Length": "28",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Cookie": response2.cookies,
"Host": "172.16.4.31",
"Origin": "http://172.16.4.31",
"Referer": url3,
"User-Agent": "Mozilla/5.0 (Linux; Android 10; SM-G981B) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/80.0.3987.162 Mobile Safari/537.36 Edg/114.0.0.0"
}
response3 = session.post(url4, data, header)
response3.encoding = 'GBK'
text4 = response3.text
url5 = "http://172.16.4.31/portalAuthAction.do"
# 函数 gethostname() 返回当前正在执行 Python 的系统主机名
res = socket.gethostbyname(socket.gethostname())
mac_address = ':'.join(hex(uuid.getnode())[2:].zfill(12)[i:i + 2] for i in range(0, 12, 2))
data1 = {
'wlanuserip': res,
'wlanacname': 'Stud_wlan_1',
'chal_id': '',
'chal_vector': '',
'auth_type': 'PAP',
'seq_id': '',
'req_id': '',
'wlanacIp': '172.16.4.67', # 更改为自己的无线网接入IP地址
'ssid': '',
'vlan': '38751184',
'mac': mac_address,
'message': '',
'bank_acct': '',
'isCookies': '',
'version': '0',
'authkey': 'amnoon',
'url': '',
'usertime': '0',
'listpasscode': '0',
'listgetpass': '0',
'getpasstype': '1',
'randstr': '6284',
'domain': '',
'isRadiusProxy': 'false',
'usertype': '0',
'isHaveNotice': '0',
'times': '12',
'weizhi': '0',
'smsid': '1',
'freeuser': '',
'freepasswd': '',
'listwxauth': '0',
'templatetype': '2',
'tname': '1',
'logintype': '0',
'act': '',
'is189': 'false',
'terminalType': '',
'checkterminal': 'true',
'portalpageid': '1',
'listfreeauth': '0',
'viewlogin': '1',
'userid': '#####', # 修改为自己的用户名
'wisprpasswd': '',
'twocode': '0',
'authGroupId': '',
'alipayappid': '',
'wlanstalocation': '',
'wlanstamac': '',
'wlanstaos': '',
'wlanstahardtype': '',
'smsoperatorsflat': '0',
'reason': '',
'res': '',
'userurl': '',
'challenge': '',
'uamip': '',
'uamport': '',
'toqrcode': '',
'isIOSPortal': 'false',
'listwxsysauth': '0',
'useridtemp': '####', # 修改为自己的用户名
'passwd': '#####', # 修改为自己的密码
'isRemind': 'on',
'lg': '%B5%E3%BB%F7%B5%C7%C2%BC',
'read': 'on'
}
header1 = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/'
'apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Content-Length': '899',
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': response2.cookies,
'Host': '172.16.4.31',
'Origin': 'http://172.16.4.31',
'Referer': url3,
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; SM-G981B) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/80.0.3987.162 Mobile Safari/537.36 Edg/114.0.0.0'
}
response4 = session.post(url5, data1, header1)
response4.encoding = 'GBK'
text5 = response4.text
print(text5)
精简版代码
import requests
import re
import socket
import uuid
# 重定向
url5 = "http://172.16.4.31/portalAuthAction.do"
# 函数 gethostname() 返回当前正在执行 Python 的系统主机名
res = socket.gethostbyname(socket.gethostname())
mac_address = ':'.join(hex(uuid.getnode())[2:].zfill(12)[i:i + 2] for i in range(0, 12, 2))
data1 = {
'wlanuserip': res,
'wlanacname': 'Stud_wlan_1',
'chal_id': '',
'chal_vector': '',
'auth_type': 'PAP',
'seq_id': '',
'req_id': '',
'ssid': '',
'vlan': '38751184',
'mac': mac_address,
'message': '',
'bank_acct': '',
'isCookies': '',
'version': '0',
'authkey': 'amnoon',
'url': '',
'usertime': '0',
'listpasscode': '0',
'listgetpass': '0',
'getpasstype': '1',
'randstr': '6284',
'domain': '',
'isRadiusProxy': 'false',
'usertype': '0',
'isHaveNotice': '0',
'times': '12',
'weizhi': '0',
'smsid': '1',
'freeuser': '',
'freepasswd': '',
'listwxauth': '0',
'templatetype': '2',
'tname': '1',
'logintype': '0',
'act': '',
'is189': 'false',
'terminalType': '',
'checkterminal': 'true',
'portalpageid': '1',
'listfreeauth': '0',
'viewlogin': '1',
'userid': '####', # 修改为自己的用户名
'wisprpasswd': '',
'twocode': '0',
'authGroupId': '',
'alipayappid': '',
'wlanstalocation': '',
'wlanstamac': '',
'wlanstaos': '',
'wlanstahardtype': '',
'smsoperatorsflat': '0',
'reason': '',
'res': '',
'userurl': '',
'challenge': '',
'uamip': '',
'uamport': '',
'toqrcode': '',
'isIOSPortal': 'false',
'listwxsysauth': '0',
'useridtemp': '####', # 修改为自己的用户名
'passwd': '####', # 修改为自己的密码
}
header1 = {
'Host': '172.16.4.31',
'Origin': 'http://172.16.4.31',
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; SM-G981B) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/80.0.3987.162 Mobile Safari/537.36 Edg/114.0.0.0'
}
response4 = requests.post(url5, data1, header1)
response4.encoding = 'GBK'
text5 = response4.text
print(text5)
图书馆端(GuizhouUnivIPv6)
有了宿舍端的经验,我只写了精简版代码
需要注意的是在图书馆端,我并没有写GuizhouUniversity的自动认证代码,若要在图书馆实现自动认证,请连接GuizhouUnivIPv6网络。
精简版代码
import requests
import re
import socket
import uuid
# 重定向
url5 = "http://172.16.4.36/portalAuthAction.do"
# 函数 gethostname() 返回当前正在执行 Python 的系统主机名
res = socket.gethostbyname(socket.gethostname())
mac_address = ':'.join(hex(uuid.getnode())[2:].zfill(12)[i:i + 2] for i in range(0, 12, 2))
data1 = {
'wlanuserip': res,
'wlanacname': 'stu_com3',
'auth_type': 'PAP',
'vlan': '1003',
'version': '0',
'authkey': 'amnoon',
'url':'http://',
'usertime': '0',
'listpasscode': '0',
'listgetpass': '0',
'getpasstype': '1',
'randstr': '6821',
'isRadiusProxy': 'false',
'usertype': '0',
'isHaveNotice': '0',
'times': '12',
'weizhi': '0',
'smsid': '1',
'listwxauth': '0',
'templatetype': '2',
'tname': '1',
'logintype': '0',
'is189': 'false',
'checkterminal': 'true',
'portalpageid': '1',
'listfreeauth': '0',
'viewlogin': '1',
'userid': '####', # 修改为自己的用户名
'twocode': '0',
'smsoperatorsflat': '0',
'isIOSPortal': 'false',
'listwxsysauth': '0',
'useridtemp': '####', # 修改为自己的用户名
'passwd': '####', # 修改为自己的密码
}
header1 = {
'Host': '172.16.4.36',
'Origin': 'http://172.16.4.36',
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; SM-G981B) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/80.0.3987.162 Mobile Safari/537.36 Edg/114.0.0.0'
}
response4 = requests.post(url5, data1, header1)
response4.encoding = 'GBK'
text5 = response4.text
print(text5)
校园网下线
由于宿舍和图书馆的校园网认证IP不同,下线程序也不同
注意
无论是宿舍校园网GuizhouUniversity还是图书馆校园网GuizhouUnivIPv6,下线都需要提供wlanacip,但该IP获取较为困难,因此下述所有下线程序都不建议使用。wlanacip可以通过使用浏览器F12开发人员选项--网络--保存日志,在浏览器执行下线过程,捕获此过程中发送接收的URL获得。
宿舍端
import requests
import re
import socket
import uuid
# 重定向
url5 = "http://172.16.4.31/portalDisconnAction.do"
# 函数 gethostname() 返回当前正在执行 Python 的系统主机名
res = socket.gethostbyname(socket.gethostname())
mac_address = ':'.join(hex(uuid.getnode())[2:].zfill(12)[i:i + 2] for i in range(0, 12, 2))
data1 = {
'portaltype':'' ,
'wlanuserip': res,
'wlanacname': 'Stud_wlan_1',
'chal_id': '',
'chal_vector': '',
'auth_type': 'PAP',
'wlanacIp': '172.16.4.67',#修改为自己的wlan接入点IP,此IP为必填项
'seq_id':'' ,
'req_id':'' ,
'ssid':'' ,
'vlan': '38751184',
'mac': mac_address,
'bank_acct': '',
'isCookies': '',
'version': '0',
'authkey': 'amnoon',
'url': 'http://www.gzu.edu.cn/',
'usertime': '299997',
'listpasscode': '0',
'listgetpass': '0',
'getpasstype': '1',
'randstr': '7316',
'domain':'' ,
'isRadiusProxy': 'false',
'usertype': '0',
'isHaveNotice': '0',
'times': '12',
'weizhi': '0',
'smsid': '1',
'freeuser':'' ,
'freepasswd':'' ,
'listwxauth': '0',
'templatetype': '2',
'tname': '1',
'logintype': '0',
'act': 'DISCONN',
'is189': 'false',
'terminalType':'' ,
'checkterminal': 'true',
'portalpageid': '1',
'listfreeauth': '0',
'viewlogin': '1',
'userid': '####',#修改为自己的用户名
'wisprpasswd': '####',#修改为自己的密码
'twocode': '0',
'authGroupId': '',
'alipayappid':'',
'wlanstalocation':'' ,
'wlanstamac':'' ,
'wlanstaos': '',
'wlanstahardtype': '',
'smsoperatorsflat': '0',
'reason': '',
'res': '',
'userurl': '',
'challenge': '',
'uamip': '',
'uamport': '',
'toqrcode': '',
'isIOSPortal': 'false',
'listwxsysauth': '0',
'logout': '%C0%EB%CF%DF'
}
header1 = {
'Host': '172.16.4.31',
'Origin': 'http://172.16.4.31',
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; SM-G981B) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/80.0.3987.162 Mobile Safari/537.36 Edg/114.0.0.0'
}
response4 = requests.post(url5, data1, header1)
response4.encoding = 'GBK'
text5 = response4.text
print(text5)
图书馆端
import requests
import re
import socket
import uuid
# 重定向
url5 = "http://172.16.4.36/portalDisconnAction.do"
# 函数 gethostname() 返回当前正在执行 Python 的系统主机名
res = socket.gethostbyname(socket.gethostname())
mac_address = ':'.join(hex(uuid.getnode())[2:].zfill(12)[i:i + 2] for i in range(0, 12, 2))
data1 = {
'portaltype':'' ,
'wlanuserip': res,
'wlanacname': 'stu_com3',
'chal_id': '',
'chal_vector': '',
'auth_type': 'PAP',
'seq_id':'' ,
'req_id':'' ,
'wlanacIp': '172.16.4.64',#更改为自己的wlanacip
'ssid':'' ,
'vlan': '1003',
'bank_acct': '',
'isCookies': '',
'version': '0',
'authkey': 'amnoon',
'url': 'http://www.gzu.edu.cn/',
'usertime': '299997',
'listpasscode': '0',
'listgetpass': '0',
'getpasstype': '1',
'randstr': '5916',
'domain':'' ,
'isRadiusProxy': 'false',
'usertype': '0',
'isHaveNotice': '0',
'times': '12',
'weizhi': '0',
'smsid': '1',
'freeuser':'' ,
'freepasswd':'' ,
'listwxauth': '0',
'templatetype': '2',
'tname': '1',
'logintype': '0',
'act': 'DISCONN',
'is189': 'false',
'terminalType':'' ,
'checkterminal': 'true',
'portalpageid': '1',
'listfreeauth': '0',
'viewlogin': '1',
'userid': '####',#更改为自己的用户名
'wisprpasswd': '####',#更改为自己的密码
'twocode': '0',
'authGroupId': '',
'alipayappid':'',
'wlanstalocation':'' ,
'wlanstamac':'' ,
'wlanstaos': '',
'wlanstahardtype': '',
'smsoperatorsflat': '0',
'reason': '',
'res': '',
'userurl': '',
'challenge': '',
'uamip': '',
'uamport': '',
'toqrcode': '',
'isIOSPortal': 'false',
'listwxsysauth': '0',
'logout': '%C0%EB%CF%DF'
}
header1 = {
'Host': '172.16.4.36',
'Origin': 'http://172.16.4.36',
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; SM-G981B) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/80.0.3987.162 Mobile Safari/537.36 Edg/114.0.0.0'
}
response4 = requests.post(url5, data1, header1)
response4.encoding = 'GBK'
text5 = response4.text
print(text5)
打包成exe文件
Python脚本可以使用pyinstaller
1.在含有Python环境的情况下,输入以下代码安装pyinstaller库:
pip install pyinstaller
2.执行命令
Pyinstaller -F 文件名.py
将在当前目录生成一个文件夹,文件夹内含有与上述文件名同名的exe文件,注意,此代码中的-F中的F必须要大写。
自动认证
完成上述步骤后,电脑连接至网络后,双击exe可执行文件即可实现WiFi自动认证。
也可以设置exe文件开机自启动,更加方便
总结
本代码的实现原理与之前“移动端”的相同,只是在Windows这一平台上需要换一种形式实现POST请求。本教程建立在Windows操作系统上,Linux/MAC操作系统请自行测试代码是否可以正常使用。