本帖最后由 ciasdmxhxjjpd@c 于 2022-6-25 12:05 编辑
RT,
路由器的信息获取,有现成的,但光猫信息的获取,资料很少。
今天分享一个获取华为光猫信息的方法。大家可以按照这个思路来获取其他类型的光猫信息。
以获取 ma5671,最新205 固件的 wanip、温度、光模块发射功率(其他信息类推)为例:
pycharm 调试
import os
import pickle
import requests
import re
import base64
import json
import urllib.parse
import codecs
DOMAIN = 'http://192.168.1.1/'
USERNAME = 'telecomadmin'
PASSWORD = 'nE7jA%5m'
HEADERS = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding': 'gzip, deflate'
}
# get random
def get_random(session):
cnt = session.post(url=DOMAIN+'asp/GetRandCount.asp').content.decode('utf-8-sig')
return cnt
# get login cookie
def get_cookie(session, cnt):
payload = {'x.X_HW_Token' : cnt}
password_decode = base64.b64encode(str.encode(PASSWORD)).decode('ascii')
cookies_dict = {'Cookie': 'Cookie=UserName:' + USERNAME + ':PassWord:' + password_decode + ':Language:english:id=-1' }
session.post(url=DOMAIN+'login.cgi', data=payload, cookies=cookies_dict)
# get ont temperature
def get_opticinfo(session):
x = session.get(url=DOMAIN+'html/amp/opticinfo/opticinfo.asp').content.decode('utf-8-sig')
opticinfos = re.findall('var opticInfos = new Array\(new stOpticInfo\("InternetGatewayDevice.X_HW_DEBUG.AMP.Optic","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)"\),null\);',x)
return opticinfos
# get pppoe wan ip
def get_wanip(session):
x = session.get(url=DOMAIN + 'html/bbsp/common/wan_list.asp').content.decode('utf-8-sig')
wanips = re.findall(
'var PPPWanList = new Array\(new WanPPP\("InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","your_adsl_account"',
x)
return wanips
# using session
session = requests.Session()
session.headers = HEADERS
# begin
get_cookie(session, get_random(session))
x = session.get(url=DOMAIN + 'frame.asp')
#y = session.get(url=DOMAIN + 'html/ssmp/deviceinfo/deviceinfo.asp')
#z = session.get(url=DOMAIN + 'html/ssmp/reset/reset.asp')
#u = session.get(url=DOMAIN + 'html/ssmp/common/StartFileLoad.asp')
#v = session.post(url=DOMAIN + 'html/ssmp/reset/set.cgi?x=InternetGatewayDevice.X_HW_DEBUG.SMP.DM.ResetBoard&RequestFile=html/ssmp/reset/reset.asp')
# opticinfo
opticinfo = get_opticinfo(session)
# wanip
wanip = get_wanip(session)
unescaped = urllib.parse.unquote(wanip[0][1])
raw_bytes = bytes(unescaped, "utf-8")
decoded = codecs.escape_decode(raw_bytes)[0].decode("utf-8")
# json out
js = {
"Optical_Temperature" : opticinfo[0][3],
"RX_Optical_Power": opticinfo[0][1],
"wan_ip": decoded
}
print(json.dumps(js))
HA 里
- platform: command_line
name: XPON Temp
command: "python /config/get_ont_info.py"
value_template: "{{ value_json.Optical_Temperature}}"
unit_of_measurement: "°C"
scan_interval: 310
- platform: command_line
name: Optical Rx Power
command: "python /config/get_ont_info.py"
value_template: "{{ value_json.RX_Optical_Power}}"
unit_of_measurement: "dBm"
scan_interval: 320
- platform: command_line
name: WAN IP
command: "python /config/get_ont_info.py"
value_template: "{{ value_json.wan_ip}}"
scan_interval: 300
效果图:
|