Skip to content

Instantly share code, notes, and snippets.

@0neday
Last active Jun 24, 2022
Embed
What would you like to do?
get huawei ont temperature using python for homeassisstant command platform
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))
import os
import pickle
import requests
import re
import base64
DOMAIN = 'http://192.168.1.1/'
USERNAME = 'yourusername'
PASSWORD = 'yourpassword'
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.get(url=DOMAIN+'asp/GetRandCount.asp').content.decode('utf-8-sig')
return cnt
# get login cookie
def get_cookie(session, cnt):
payload = {'UserName': USERNAME, 'PassWord': base64.b64encode(str.encode(PASSWORD)),'Language': 'chinese', 'x.X_HW_Token' : cnt}
save_cookies(session.post(url=DOMAIN+'login.cgi', data=payload).cookies,'cookie.txt')
# get ont temperature
def get_opticinfo(session):
x = session.get(url=DOMAIN+'html/amp/opticinfo/opticinfo.asp', cookies=load_cookies('cookie.txt')).content.decode('utf-8-sig')
opticinfos = re.findall('var opticInfos = new Array\(new stOpticInfo\("InternetGatewayDevice.X_HW_DEBUG.AMP.Optic","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)"\),null\);',x)
return opticinfos
def save_cookies(cookiejar, filename):
with open(filename, 'wb') as f:
pickle.dump(cookiejar, f)
def load_cookies(filename):
with open(filename, 'rb') as f:
return pickle.load(f)
session = requests.Session()
session.headers = HEADERS
if not os.path.isfile('cookie.txt'):
get_cookie(session, get_random(session))
opticinfo = get_opticinfo(session)
if not opticinfo:
get_cookie(session, get_random(session))
opticinfo_tmp = get_opticinfo(session)
print(opticinfo_tmp[0][4])
else:
print(opticinfo[0][4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment