首页 > 其他分享 >电脑自动连接华科校园网

电脑自动连接华科校园网

时间:2022-12-10 20:56:20浏览次数:75  
标签:import 电脑自动 content time 校园网 华科 login ping

电脑自动连接华科校园网

校园网自动连接开机连接

源码及分析

每个5秒检查自己还能不能上网, 方式就是ping 8.8.8.8, 这是很常见的方法。如果不能上网, 那么就调用login()函数来自动重连。

我们的校园网认证的过程,本质上就是提交一个HTTP请求, 那我们的login()函数, 其实就是实现了这个过程, 因此我们只需要知道,我们的网页端认证是怎么操作的即可,麻烦的是华科的校园网的代码设计的还是很复杂的,所以还是稍微的耗费了一点时间。

  1. import time 
  2. import json 
  3.  
  4. from scapy.layers.inet import IP, ICMP 
  5. from scapy.packet import Raw 
  6. from scapy.sendrecv import sr1 
  7.  
  8. import requests 
  9.  
  10. def login(): 
  11. url = 'http://192.168.50.3:8080/eportal/InterFace.do?method=login' 
  12. with open("content", "r") as f: 
  13. data = f.read() 
  14. header = { 
  15. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" 

  16. response = requests.post(url, data, headers=header) 
  17. content = json.loads(response.text) 
  18. encoding = response.encoding 
  19. if content['result'] == 'fail': 
  20. print(content['message'].encode(encoding).decode('utf-8')) 
  21. else: 
  22. print("login at --> " + time.asctime(time.localtime(time.time()))) 
  23. return 
  24.  
  25. def pong(): 
  26. ping_pkt = IP(dst="8.8.8.8") / ICMP() / b'welcome!' 
  27. ping_result = sr1(ping_pkt, timeout=2, verbose=False) 
  28. try: 
  29. if ping_result.getlayer(ICMP).fields['type'] == 0 \ 
  30. and ping_result.getlayer(Raw).fields['load'] == b'welcome!': 
  31. return True 
  32. else: 
  33. return False 
  34. except Exception: 
  35. return False 
  36.  
  37. if __name__ == '__main__': 
  38. while True: 
  39. if pong(): 
  40. time.sleep(5) 
  41. else: 
  42. login() 
  43. time.sleep(10) 

校园网认证流程

首先我们进入这个认证的页面, 按F12进入开发者模式。会在Source中看到两个重要的JS文件,当我们点击网页中的连接Login的时候就会调用这个函数。

登录校园网认真网站
登录校园网认真网站
这个函数会收集你的一系列信息,并最终调用:AuthInterFace.login

enter description here
enter description here
AuthInterFace定义在AuthInterFace.js中。其中init()用于生成请求的url, login()首先将传入的参数合并成content,最后通过post方法发送请求。

enter description here
enter description here
因此需要我们做的就是, 获取content的内容, 然后我们就可以用python对指定的url发起post请求。获取content的方法也很简单, 只需要在如图位置打上断点, 然后点击网页中连接的按钮,当js执行到断点后,就可以复制content的值

获取content值
获取content值

使用

  1. git clone https://github.com/Kingdo777/auto-connect-school-network.git
  2. 我们将上一过程获取到的content信息存放到main.py的同目录下的名为content的文件中
  3. 然后就可以执行python脚本了
  4. 不过windows下需要安装npcap, 可以点击以下链接下载安装:https://nmap.org/npcap/dist/npcap-1.60.exe

电脑重启自动运行py脚本

首先在Python脚本文件夹下,创建test.bat批处理文件,其内如如下所示。

  1. cd C:\Users\99292\Desktop\login HUST 
  2. python login.py 

创建基本任务
创建基本任务

触发器这里选择是计算机启动时。

触发器选择
触发器选择
在浏览中选择批处理工程,最后选择确认即可。

选择启动程序编写的批处理
选择启动程序编写的批处理
双击刚刚创建的基本任务,选择对应的模式即可。

enter description here
enter description here

设置电脑定时开机

其余设置基本上与批处理设置任务相同,区别在于触发器选择每天,开始时间设置为早上7点时间即可。

设置自动启动
设置自动启动

后记

  1. 如果切换了Wifi, 导致IP地址改变, 那么需要重新获取content的值.

运行脚本时遇到的问题

ModuleNotFoundError: No module named ‘scapy’

  1. pip install scapy -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com 

最终文件

login.py会调用content的内容进行登录华科校园网,需要注意一点content里面的内容会由于你IP的变化而变化,因此当IP改变时,要重新修改content的内容。

test.bat时批处理脚本,用处在于电脑重启时,会调用此脚本,连接华科校园网。

文件夹文件
文件夹文件

标签:import,电脑自动,content,time,校园网,华科,login,ping
From: https://www.cnblogs.com/liuziyan0511/p/16972295.html

相关文章