import socket import sys import smtplib import subprocess import time import urllib.request from email.header import Header from email.mime.text import MIMEText
import os from tencentcloud.common import credential from tencentcloud.common.exception.tencent_cloud_sdk_exception import ( TencentCloudSDKException, )
from tencentcloud.dnspod.v20210323 import dnspod_client, models
defisNetOK(testserver): s = socket.socket() s.settimeout(3) try: status = s.connect_ex(testserver) if status == 0: s.close() returnTrue else: returnFalse except Exception as e: returnFalse
defddns_refresh(v6_address): for addr in v6_address: if"::"notin addr: continue print(f"refreshing ddns '{addr}'")
m = models.DescribeRecordListRequest() m.Domain = DOMAIN records = client.DescribeRecordList(m) for record in records.RecordList: # remove all records if record.Type == "AAAA": req = models.DeleteRecordRequest() req.Domain = DOMAIN req.RecordId = record.RecordId client.DeleteRecord(req)
# add new record for addr req = models.CreateRecordRequest() req.Domain = DOMAIN req.RecordType = "AAAA" req.Value = addr req.RecordLine = "默认"
defget_v6_address(): cmd_output = subprocess.run(cmd, capture_output=True).stdout.decode(encoding) v6_address = set() for line in cmd_output.splitlines(): if splitter in line and"fe80"notin line: addr = line.split(splitter)[1].strip() addr = addr.split("/64")[0] if addr == "::1": continue v6_address.add(addr) return v6_address
defset_to_str(sets): return"\n".join(sets)
if __name__ == "__main__": whilenot isNetChainOK(): time.sleep(10) print("No network connection, waiting...") v6_address = get_v6_address() ddns_refresh(v6_address) for e in send_list: print("设备联网,开始监测V6地址") whileTrue: time.sleep(60) new_v6_address = get_v6_address() if isNetChainOK() and new_v6_address and new_v6_address != v6_address: try: ddns_refresh(new_v6_address) except Exception as e: print(e) print("error refreshing ddns...") v6_address = new_v6_address