自动登录wifi的python脚本

爬校园网登录的接口实现自动登录

前言

最近买了笔记本,每次登录学校wifi都会弹出portal让我登录,非常麻烦,于是想写个python自动登录

抓包分析

打开charles——我最爱的抓包软件,点击登录,可以找到这个登录button向172.30.21.100的接口发送了post请求 image 仔细分析一下数据 好像没什么要分析的 image-1677563261417 是一个post请求加上form表单 分析到此直接写代码就行了

python代码实现

 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
import requests
import time
## 一个自动登录WLAN程序
class AutoLoginWLAN:
    def testConnection(self):
        res = requests.get("https://ipw.cn")
        if res.status_code != 200:
            # self.login()
            return False
        else:
            return True
    def login(self):
        headers = {
            "Host":"172.30.21.100",
            "Origin":"http://172.30.21.100",
            "Referer":"http://172.30.21.100/tpl/whut/login.html?nasId=14",
            "Content-Type":"application/x-www-form-urlencoded"
        }
        data = {
            "username":"xxxxxx",
            "password":"xxxxxx",
            "nasId":14
        }
        res = requests.post("http://172.30.21.100/api/account/login",data=data)        
if __name__ == "__main__":
    loginProcess = AutoLoginWLAN()
    while True:
        if loginProcess.testConnection():
            loginProcess.login()
            time.sleep(5) # 五秒重复一次登录
        else:
            time.sleep(300) # 5分钟检查一次是否登录

打包成exe

使用pyinstaller打包 命令行

1
pyinstaller autologinwlan.py -F -w

-F打包成单个文件,-w关闭命令行输出选项

放到启动里自动启动

image-1677563449461 多余的解释 image-1677563507958

后记

懒人是这样的 github项目地址自动登录wifi