连载:自动连接公共WIFI(一):连接方法分析
自动登录脚本思路如下:
- 循环访问www.baidu.com网址,如果成功获取到百度页面,则网络在线。
- 若1中返回的是WIFI认证页面,则解析该链接,获取我们需要的参数。
- 拼接认证页面需要的参数,提交WIFI认证,解析返回结果判断是否认证成功。
需要引入requests包,访问baidu页面:
import requests import time response = reque(";) re = "utf-8" if "百度" in re: # 成功获取到百度首页 prin("%Y-%m-%d-%H:%M:%S", ()), "网络在线")
如果返回的是WIFI认证页面,解析认证页面链接中的参数:
# wifi认证redirect页面,截取wifi认证url和参数(IP、MAC等) # redirectUrl = '<script type="text/javascript">loca;;userip=10.131.50.166&ssid=FREE%2dWIFI&nasip=10%2e131%2e6%2e1&devType=v7"</script>Authentication is required. Click <a href=";userip=10.131.50.166&ssid=FREE%2dWIFI&nasip=10%2e131%2e6%2e1&devType=v7">here</a> to open the authentication page.' redirectUrl = re.replace("\n", "") #使用正则表达式获取参数 matchObjects = re.match(r'.*loca;(http://.*)wsmAuth.*usermac=(.*)&userip=(.*)&ssid=(.*)&nasip=(.*)&devType=(.*)"</script>', redirectUrl, re.M) if matchObjects: baseUrl = unquote(1)) usermac = unquote(2)) userip = unquote(3)) ssid = unquote(4)) nasip = unquote(5)) devType = unquote(6))
拼接认证参数并提交认证:
# 模拟提交用户名密码登录 authResponse = reque(baseUrl+"wsmAuth/login", data={"userName":"135xxxxxxxx","signature":"XXXXXXXX","usermac":usermac,"userip":userip,"ssid":ssid,"nasip":nasip,"devType":devType,"operateType":"7","userAgreement":"1","rabbit":"h3c","templateId":"7","shopid":"1","groupid":"0","authCfgid":"1","ipmAuthType":"1","authPage":"/themeTemplate/1638770462964;,"onebutton":"0","needWechat":"1"}) if "succurl" in authRe: prin("%Y-%m-%d-%H:%M:%S", ()), "WIFI认证成功") else: prin("%Y-%m-%d-%H:%M:%S", ()), "WIFI认证失败", au, authRe)
完整代码如下:
import requests from urllib.parse import unquote import time import re while True: try: sleepTime = 60 response = reque(";) re = "utf-8" if "百度" in re: # 成功获取到百度首页 prin("%Y-%m-%d-%H:%M:%S", ()), "网络在线") else: prin("%Y-%m-%d-%H:%M:%S", ()), "网络不在线,尝试连接WIFI") # wifi认证redirect页面,截取wifi认证url和参数(IP、MAC等) # redirectUrl = '<script type="text/javascript">loca;;userip=10.131.50.166&ssid=FREE%2dWIFI&nasip=10%2e131%2e6%2e1&devType=v7"</script>Authentication is required. Click <a href=";userip=10.131.50.166&ssid=FREE%2dWIFI&nasip=10%2e131%2e6%2e1&devType=v7">here</a> to open the authentication page.' redirectUrl = re.replace("\n", "") #使用正则表达式获取参数 matchObjects = re.match(r'.*loca;(http://.*)wsmAuth.*usermac=(.*)&userip=(.*)&ssid=(.*)&nasip=(.*)&devType=(.*)"</script>', redirectUrl, re.M) if matchObjects: baseUrl = unquote(1)) usermac = unquote(2)) userip = unquote(3)) ssid = unquote(4)) nasip = unquote(5)) devType = unquote(6)) # 模拟提交用户名密码登录 authResponse = reque(baseUrl+"wsmAuth/login", data={"userName":"135xxxxxxxx","signature":"XXXXXXXX","usermac":usermac,"userip":userip,"ssid":ssid,"nasip":nasip,"devType":devType,"operateType":"7","userAgreement":"1","rabbit":"h3c","templateId":"7","shopid":"1","groupid":"0","authCfgid":"1","ipmAuthType":"1","authPage":"/themeTemplate/1638770462964;,"onebutton":"0","needWechat":"1"}) if "succurl" in authRe: prin("%Y-%m-%d-%H:%M:%S", ()), "WIFI认证成功") else: prin("%Y-%m-%d-%H:%M:%S", ()), "WIFI认证失败", au, authRe) sleepTime = 10 else: prin("%Y-%m-%d-%H:%M:%S", ()), "Redirect Location解析失败") sleepTime = 10 (sleepTime) except KeyboardInterrupt: # 退出循环 break except Exception as e: # 异常可能是无线网络未连接,继续等待即可 sleepTime = 10 prin("%Y-%m-%d-%H:%M:%S", ()), f"网络连接异常,等待{sleepTime}秒") (sleepTime)