本帖最后由 ciasdmxhxjjpd@c 于 2022-6-5 16:49 编辑
RT,
最近 发现重启路由后,ddns更新老是失败,查看了下流程,显示 unkonwn,说明 dns 解析失败了!
楼主,用的是 通过 dnsip 更新 cf ddns,https://bbs.hassbian.com/thread-16149-1-1.html
改成运营商的dns,解析不稳定,也存在污染。
所以,想了下,用命令行吧,直接从vps隧道获取解析地址吧,vps的dns 绑定在5300
sensor:
- platform: command_line
name: myip
command: "python /config/myip.py"
scan_interval: 300
- platform: command_line
name: yourdomain
command: "dig yourdomain @127.0.0.1 -p 5300 +short"
scan_interval: 120
因,楼主是通过光猫拨号,n1旁路路由,所以,要通过 python 脚本获取 光猫拨号之后的 pppoe wanip(华为hs8145x6),当然,你也可以通过其他方式,楼主主要是不想泄露自己的ip。
import os
import pickle
import requests
import re
import base64
import urllib.parse
import codecs
DOMAIN = 'http://192.168.1.1:8080/'
USERNAME = 'user'
PASSWORD = 'password'
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 pppoe wan ip
def get_wanip(session):
x = session.get(url=DOMAIN + 'html/bbsp/common/wan_list.asp', cookies=load_cookies('cookie.txt')).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
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))
wanip = get_wanip(session)
if not wanip:
get_cookie(session, get_random(session))
wanip_tmp = get_wanip(session)
unescaped = urllib.parse.unquote(wanip_tmp[0][1])
else:
unescaped = urllib.parse.unquote(wanip[0][1])
raw_bytes = bytes(unescaped, "utf-8")
decoded = codecs.escape_decode(raw_bytes)[0].decode("utf-8")
print(decoded)
增加上面的支路,是为了确保能100%更新成功。
|