我这边的校园网使用强制门户来实现登录。
当要求你通过浏览器登录时,按F12打开控制台,选中网络,找到那个发送你学号、密码的请求,右键,复制为curl,我这边的复制结果如下所示(******
为打码内容):
curl --silent 'http://10.7.0.103:30004/byod/byodrs/login/defaultLogin' --compressed -X POST -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: zh-CN,en-US;q=0.7,en;q=0.3' -H 'Accept-Encoding: gzip, deflate' -H 'Content-Type: application/json' -H 'X-Requested-With: XMLHttpRequest' -H 'Origin: http://10.7.0.103:30004' -H 'Connection: keep-alive' -H 'Referer: http://10.7.0.103:30004/byod/view/byod/template/templatePc.html?customId=26&nasRedirectUrl=http://10.7.0.103:30004/byod/index.html?usermac=******&userip=10.121.3.156&userurl=http://detectportal.firefox.com/canonical.html&original=http://detectportal.firefox.com/canonical.html&ssid=E' -H 'Cookie: testcookie=yes; userip=10.121.3.156' -H 'DNT: 1' -H 'Sec-GPC: 1' --data-raw '{"userName":"******","userPassword":"******","serviceSuffixId":"-1","dynamicPwdAuth":false,"code":"","codeTime":"","validateCode":"","licenseCode":"","userGroupId":0,"validationType":0,"guestManagerId":0,"shopIdE":null,"wlannasid":null}')
打开控制台,粘贴你复制的东西,回车,应该会返回“成功登录校园网!”之类的结果。
确认通过curl发送这个请求可以做到登录校园网后,接下来你需要做的就是让你电脑在登录时自动运行一下这段命令。
下面以 GNU/Linux 为例,使用systemd做到用户登录时自动执行某个服务:
先把上面代码转换成脚本的形式,此时可以请ChatGPT加一个登录失败自动重试逻辑:
校园网登录.sh
#!/bin/bash
while true
do
response=$(curl --silent 'http://10.7.0.103:30004/byod/byodrs/login/defaultLogin' --compressed -X POST -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: zh-CN,en-US;q=0.7,en;q=0.3' -H 'Accept-Encoding: gzip, deflate' -H 'Content-Type: application/json' -H 'X-Requested-With: XMLHttpRequest' -H 'Origin: http://10.7.0.103:30004' -H 'Connection: keep-alive' -H 'Referer: http://10.7.0.103:30004/byod/view/byod/template/templatePc.html?customId=26&nasRedirectUrl=http://10.7.0.103:30004/byod/index.html?usermac=******&userip=10.121.3.156&userurl=http://detectportal.firefox.com/canonical.html&original=http://detectportal.firefox.com/canonical.html&ssid=E' -H 'Cookie: testcookie=yes; userip=10.121.3.156' -H 'DNT: 1' -H 'Sec-GPC: 1' --data-raw '{"userName":"******","userPassword":"******","serviceSuffixId":"-1","dynamicPwdAuth":false,"code":"","codeTime":"","validateCode":"","licenseCode":"","userGroupId":0,"validationType":0,"guestManagerId":0,"shopIdE":null,"wlannasid":null}')
code=$(echo $response | jq -r .code)
if [ $code -eq 0 ]
then
echo "成功登录校园网!"
exit 0
else
echo $response
echo "登录失败,Retrying in 1 minute..."
sleep 60
fi
done
然后参考archwiki/Systemd/用户,在~/.config/systemd/user
加一个校园网登录.service
文件:
校园网登录.service
[Unit]
Description=校园网自动登录
After=network.target
[Service]
ExecStart=sh ./校园网登录.sh
WorkingDirectory=%h/Program/运维自动化
[Install]
WantedBy=default.target
然后 systemctl --user daemon-reload
让 systemd 用户实例检测到 校园网登录
这个服务。
先 systemctl --user start 校园网登录.service
单次启动 校园网登录
服务,然后,查看状态:
systemctl --user status 校园网登录.service
○ 校园网登录.service - 校园网自动登录
Loaded: loaded (/home/voyage200/.config/systemd/user/zhbit_net.service; disabled; preset: enabled)
Active: inactive (dead) since Sun 2024-11-24 17:44:53 CST; 2min 49s ago
Duration: 258ms
Invocation: 49c7ae4fd0074bccbf3b04d7953941bb
Process: 31271 ExecStart=sh ./校园网登录.sh (code=exited, status=0/SUCCESS)
Main PID: 31271 (code=exited, status=0/SUCCESS)
Mem peak: 2.3M
CPU: 14ms
11月 24 17:44:53 VC200 systemd[639]: Started 校园网自动登录.
11月 24 17:44:53 VC200 sh[31271]: 成功登录校园网!
通过日志可以观察到脚本正常发送了请求,并接收到了来自校园网认证服务器的成功登录的回应。
确认服务正常运行后,我们 systemctl --user enable 校园网登录.service
就大功告成了。
Tips :systemctl --user enable --now 校园网登录.service
相当于先 enable校园网登录.service
后 start校园网登录.service
。
对于 windows 用户只需要让GPT把上面脚本由.sh
转换成.ps1
即可,然后使用“任务计划程序”也能做到登录后自动执行脚本的效果。