import configparser import requests import sys import os.path as osp class GetRouterWanIP: def __init__(self): self.ip = None self.username = None self.password = None self.stok = None self.remain_time = None self.cookie = None self.error_time = None self.get_router_info() @staticmethod def check_info_exist(): if not osp.exists("/home/marques/software/ddns/router_info.ini"): print("Error: Router information config not exist!") print("Info: Please fill the configuration file ./router_info.ini") info_config = configparser.ConfigParser() info_config["DEFAULT"] = {"ip": "", "username": "", "password": "", "error_time": 0} with open("/home/marques/software/ddns/router_info.ini", "w") as file: info_config.write(file) def get_router_info(self): router_info_config = configparser.ConfigParser() router_info_config.read("/home/marques/software/ddns/router_info.ini", encoding="utf-8") self.ip = router_info_config["DEFAULT"]["ip"] self.username = router_info_config["DEFAULT"]["username"] self.password = router_info_config["DEFAULT"]["password"] self.error_time = int(router_info_config["DEFAULT"]["error_time"]) if self.error_time > 0: print("please check your password and manually reset error_time's value to 0 in the router_info_ini") sys.exit(0) def get_stok(self): # 获取stok get_stok_url = 'http://{}/cgi-bin/turbo/admin_web/login_admin?username={}&password={}'.format(self.ip, self.username, self.password) stok_response = requests.get(get_stok_url) # print(stok_response.json()) # self.stok = stok_response.json()["stok"] self.remain_time = stok_response.json()["remaining_num"] # print(self.remain_time) if self.remain_time != 10: router_info_config = configparser.ConfigParser() router_info_config.read("./router_info.ini", encoding="utf-8") router_info_config["DEFAULT"]["error_time"] = str(10-self.remain_time) with open("./router_info.ini", "w") as file: router_info_config.write(file) print("please check your password and manually reset error_time's value to 0 in the router_info_ini") sys.exit(0) self.stok = stok_response.json()["stok"] self.cookie = requests.utils.dict_from_cookiejar(stok_response.cookies) def get_wan_ip(self): wan_ip_url = "http://{}/cgi-bin/turbo{}/proxy/call".format(self.ip, self.stok) params = {"_roundRobinUpdateD": ""} headers = { "Host": self.ip, "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0", "Accept": "application/json, text/javascript, */*; q=0.01", "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2", "Accept-Encoding": "gzip, deflate", "Content-Type": "application/json", "X-Requested-With": "XMLHttpRequest", "X-KL-Ajax-Request": "Ajax_Request", "Origin": "http://{}".format(self.ip), "Connection": "keep-alive", "Referer": "http://{}/cgi-bin/turbo{}/admin_web".format(self.ip, self.stok), "Cookie": "sysauth={}".format(self.cookie['sysauth']) } post_json = {"muticall": "1", "mutiargs": [{"method": "wan.get_status", "data": {}}], "lang": "zh-CN", "version": "v1"} wan_ip_response = requests.post(wan_ip_url, headers=headers, params=params, json=post_json) wan_ip = wan_ip_response.json()["data"]["results"][0]["result"]['data']["wan_ip"] #with open("./wan_ip.txt", "w") as file: # file.write(wan_ip) print(wan_ip) return wan_ip if __name__ == '__main__': GetRouterWanIP.check_info_exist() getRouterWanIP = GetRouterWanIP() getRouterWanIP.get_stok() getRouterWanIP.get_wan_ip()