自动登录脚本思路如下:
- 循环访问www.baidu.com网址,如果成功获取到百度页面,则网络在线。
- 若1中返回的是WIFI认证页面,则解析该链接,获取我们需要的参数。
- 拼接认证页面需要的参数,提交WIFI认证,解析返回结果判断是否认证成功。
需要引入requests包,访问baidu页面:
import requests
import time
response = requests.get("http://www.baidu.com")
response.encoding = "utf-8"
if "百度" in response.text:
# 成功获取到百度首页
print(time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()), "网络在线")
如果返回的是WIFI认证页面,解析认证页面链接中的参数:
# wifi认证redirect页面,截取wifi认证url和参数(IP、MAC等)
# redirectUrl = '<script type="text/javascript">location.href="http://10.131.6.1:8080/wsmAuth/iportal/?usermac=34-F7-16-79-XX-XX&userip=10.131.50.166&ssid=FREE%2dWIFI&nasip=10%2e131%2e6%2e1&devType=v7"</script>Authentication is required. Click <a href="http://10.131.6.1:8080/wsmAuth/iportal/?usermac=34-F7-16-79-XX-XX&userip=10.131.50.166&ssid=FREE%2dWIFI&nasip=10%2e131%2e6%2e1&devType=v7">here</a> to open the authentication page.'
redirectUrl = response.text.replace("\n", "")
#使用正则表达式获取参数
matchObjects = re.match(r'.*location.href="(http://.*)wsmAuth.*usermac=(.*)&userip=(.*)&ssid=(.*)&nasip=(.*)&devType=(.*)"</script>', redirectUrl, re.M|re.I)
if matchObjects:
baseUrl = unquote(matchObjects.group(1))
usermac = unquote(matchObjects.group(2))
userip = unquote(matchObjects.group(3))
ssid = unquote(matchObjects.group(4))
nasip = unquote(matchObjects.group(5))
devType = unquote(matchObjects.group(6))
拼接认证参数并提交认证:
# 模拟提交用户名密码登录
authResponse = requests.post(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/auth.xml","onebutton":"0","needWechat":"1"})
if "succurl" in authResponse.text:
print(time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()), "WIFI认证成功")
else:
print(time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()), "WIFI认证失败", authResponse.status_code, authResponse.text)
完整代码如下:
import requests
from urllib.parse import unquote
import time
import re
while True:
try:
sleepTime = 60
response = requests.get("http://www.baidu.com")
response.encoding = "utf-8"
if "百度" in response.text:
# 成功获取到百度首页
print(time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()), "网络在线")
else:
print(time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()), "网络不在线,尝试连接WIFI")
# wifi认证redirect页面,截取wifi认证url和参数(IP、MAC等)
# redirectUrl = '<script type="text/javascript">location.href="http://10.131.6.1:8080/wsmAuth/iportal/?usermac=34-F7-16-79-XX-XX&userip=10.131.50.166&ssid=FREE%2dWIFI&nasip=10%2e131%2e6%2e1&devType=v7"</script>Authentication is required. Click <a href="http://10.131.6.1:8080/wsmAuth/iportal/?usermac=34-F7-16-79-XX-XX&userip=10.131.50.166&ssid=FREE%2dWIFI&nasip=10%2e131%2e6%2e1&devType=v7">here</a> to open the authentication page.'
redirectUrl = response.text.replace("\n", "")
#使用正则表达式获取参数
matchObjects = re.match(r'.*location.href="(http://.*)wsmAuth.*usermac=(.*)&userip=(.*)&ssid=(.*)&nasip=(.*)&devType=(.*)"</script>', redirectUrl, re.M|re.I)
if matchObjects:
baseUrl = unquote(matchObjects.group(1))
usermac = unquote(matchObjects.group(2))
userip = unquote(matchObjects.group(3))
ssid = unquote(matchObjects.group(4))
nasip = unquote(matchObjects.group(5))
devType = unquote(matchObjects.group(6))
# 模拟提交用户名密码登录
authResponse = requests.post(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/auth.xml","onebutton":"0","needWechat":"1"})
if "succurl" in authResponse.text:
print(time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()), "WIFI认证成功")
else:
print(time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()), "WIFI认证失败", authResponse.status_code, authResponse.text)
sleepTime = 10
else:
print(time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()), "Redirect Location解析失败")
sleepTime = 10
time.sleep(sleepTime)
except KeyboardInterrupt:
# 退出循环
break
except Exception as e:
# 异常可能是无线网络未连接,继续等待即可
sleepTime = 10
print(time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()), f"网络连接异常,等待{sleepTime}秒")
time.sleep(sleepTime)