在本文中,我们将展示如何使用Python编写一个简单的脚本来实现Linux下的惠尔顿上网认证。以下是我们需要的参数和值:
wholeton_host
: 惠尔顿服务器地址,例如 '192.168.10.10'
wholeton_user
: 用户名,例如 'AABBCC'
wholeton_pass
: 密码,例如 '231313'
wholeton_ip
: 客户端IP地址,例如 '10.10.1.1'
wholeton_mac
: 客户端MAC地址,例如 'aa:bb::cc:dd:ee:ff'
update_secs
: 认证更新间隔时间(秒),例如 28800
(8小时)以下是完整的Python代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import socket
from datetime import datetime
from uuid import getnode
import urllib
try:
import urllib2
except Exception:
from urllib import request as urllib2
try:
import Cookie as cookies
except Exception:
from http import cookies
import websocket
import json
wholeton_host = '192.168.10.10'
wholeton_user = 'AABBCC'
wholeton_pass = '231313'
wholeton_ip = ''
wholeton_mac = ''
update_secs = 28800
def url_encode(obj):
try:
return urllib.urlencode(obj)
except Exception:
return urllib.parse.urlencode(obj)
def get_ip():
return '10.10.1.1'
def get_mac():
return 'aa:bb::cc:dd:ee:ff'
if not wholeton_ip:
wholeton_ip = get_ip()
if not wholeton_mac:
wholeton_mac = get_mac()
uri_keys = {'id': 0, 'url': 'mail.126.com', 'user': wholeton_ip, 'mac': wholeton_mac}
uri_data = url_encode(uri_keys).replace('%3A', ':')
auth_data = url_encode(
{'param[UserName]': wholeton_user, 'param[UserPswd]': wholeton_pass, 'uri': uri_data, 'force': 0})
# convert for python 3
if sys.version_info[0] == 3:
auth_data = auth_data.encode('ascii')
def main():
ws = None
loop = True
try:
while loop:
resp = urllib2.urlopen('http://' + wholeton_host + '/user-login-auth?' + uri_data, timeout=5, data=auth_data)
# get session cookie
cookie = cookies.SimpleCookie()
cookie.load(resp.info()['Set-Cookie'])
resp_data = resp.read()
if resp_data:
print('Login response:')
print(resp_data)
ws = websocket.WebSocket()
ws.connect('ws://' + wholeton_host + '/go-ws/user-auth',
cookie='fms_session=' + cookie.get('fms_session').value, origin='http://' + wholeton_host)
dt_start = datetime.now()
while ws:
try:
ws_data = ws.recv()
except KeyboardInterrupt:
loop = False
break
except:
break
if ws_data:
dt_now = datetime.now()
if (dt_now - dt_start).seconds >= update_secs:
break
print(dt_now)
print(ws_data)
ws_obj = json.loads(ws_data)
if ws_obj and ws_obj["type"] == "logged-out":
break
if ws:
ws.close()
ws = None
except KeyboardInterrupt:
pass
if ws:
ws.close()
if __name__ == '__main__':
main()
这个脚本首先定义了所需的参数和函数,然后在main
函数中实现了认证流程。它会周期性地向惠尔顿服务器发送认证请求,并在接收到服务器响应后解析并处理数据。
要运行此脚本,请将上述代码保存为一个.py
文件,然后在Linux终端中使用python
命令执行该文件。请注意,你需要根据实际情况修改wholeton_host
、wholeton_user
、wholeton_pass
、wholeton_ip
和wholeton_mac
等参数的值。
启动程序:
#前台启动
python3 wholeton-auth.py
python2 wholeton-auth.py
python wholeton-auth.py
#后台启动
nohup python3 wholeton-auth.py 2>&1 > /dev/null &
离线安装python依赖可参考
#离线安装Python依赖:以six和websocket-client为例
https://yjtzfywh.blog.csdn.net/article/details/135225376