自己在阿里云有域名,就不用花生壳了,写了个脚本实现动态域名解析。
#!/usr/bin/pythonimport timeimport requestsfrom alibabacloud_alidns0109 import models as alidns_modelsfrom alibabacloud_alidns0109.client import Client as AlidnsClientfrom alibabacloud_tea_openapi import models as open_api_modelsfrom lxml import etreeaccess_key_id = 'LTAIPaHC7****'access_key_secret = 'xwnEsyZcjzpJc58h*****'# 主机记录。如果要解析@.,主机记录要填写”@”,而不是空。rr = 'wwww'# 域名名称。示例值: domain_name = ''# 多长时间执行一次。秒wait_time = 10class UpdateDns:def __init__(self):pass@staticmethoddef create_client(access_key_id: str, access_key_secret: str, ) -> AlidnsClient:config = open_api_models.Config(access_key_id=access_key_id,access_key_secret=access_key_secret)# 访问的域名config.endpoint = f'-'return AlidnsClient(config)@staticmethoddef main(rr: str, domain_name: str, wait_time: int) -> None:# print('开始执行')client = UpdateDns.create_client(access_key_id, access_key_secret)describe_sub_domain_records_request = alidns_models.DescribeSubDomainRecordsRequest()describe_sub_domain_records_request.sub_domain = rr + '.' + domain_namedescribe_sub_domain_records_request.type = 'A'# 复制代码运行请自行打印 API 的返回值records = client.describe_sub_domain_records(describe_sub_domain_records_request)body = records.bodytotal_count = body.total_countdomain_records = body.domain_recordsip = UpdateDns.get_ip()if total_count == 0:print('没有记录,新增记录', ip)add_domain_record_request = alidns_models.AddDomainRecordRequest()add_domain_record_request.domain_name = domain_nameadd_domain_record_request.rr = rradd_domain_record_request.type = 'A'add_domain_record_request.value = ipresult = client.add_domain_record(add_domain_record_request)# print('新增结果', result.body)else:# print('记录存在,判断是否需要更新')domain = domain_records.record[0]value = domain.valueif value != ip:print('记录存在', value, ',值不同,需要更新', ip)update_domain_record_request = alidns_models.UpdateDomainRecordRequest()update_domain_record_request.record_id = domain.record_idupdate_domain_record_request.rr = rrupdate_domain_record_request.type = 'A'update_domain_record_request.value = ipresult = client.update_domain_record(update_domain_record_request)# print('更新结果', result.body)# print('等待', wait_time, '秒')time.sleep(wait_time)@staticmethoddef get_ip():html_data = requests.get('/static/customercare/yourip.asp')tree = etree.HTML(html_data.text)ip = tree.xpath('//h2')return ip[0].text.strip()if __name__ == '__main__':while True:UpdateDns.main(rr, domain_name, wait_time)
requirements.txt
requests==2.25.1lxml==4.7.1alibabacloud_tea_openapi==0.3.1alibabacloud_alidns0109==2.0.2
GitHub:/weiangongsi/aliyun-ddns
防火布