python脚本实现阿里云DDNS动态域名解析

自己在阿里云有域名,就不用花生壳了,写了个脚本实现动态域名解析。

#!/usr/bin/python

import time

import requests
from alibabacloud_alidns20150109 import models as alidns_models
from alibabacloud_alidns20150109.client import Client as AlidnsClient
from alibabacloud_tea_openapi import models as open_api_models
from lxml import etree

access_key_id = 'LTAIPaHC7****'
access_key_secret = 'xwnEsyZcjzpJc58h*****'
# 主机记录。如果要解析@.exmaple.com,主机记录要填写”@”,而不是空。
rr = 'wwww'
# 域名名称。示例值: example.com
domain_name = 'example.com'
# 多长时间执行一次。秒
wait_time = 10


class UpdateDns:

    def __init__(self):
        pass

    @staticmethod
    def 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'alidns.cn-hangzhou.aliyuncs.com'
        return AlidnsClient(config)

    @staticmethod
    def 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_name
        describe_sub_domain_records_request.type = 'A'
        # 复制代码运行请自行打印 API 的返回值
        records = client.describe_sub_domain_records(describe_sub_domain_records_request)
        body = records.body
        total_count = body.total_count
        domain_records = body.domain_records
        ip = UpdateDns.get_ip()
        if total_count == 0:
            print('没有记录,新增记录', ip)
            add_domain_record_request = alidns_models.AddDomainRecordRequest()
            add_domain_record_request.domain_name = domain_name
            add_domain_record_request.rr = rr
            add_domain_record_request.type = 'A'
            add_domain_record_request.value = ip
            result = client.add_domain_record(add_domain_record_request)
            # print('新增结果', result.body)
        else:
            # print('记录存在,判断是否需要更新')
            domain = domain_records.record[0]
            value = domain.value
            if value != ip:
                print('记录存在', value, ',值不同,需要更新', ip)
                update_domain_record_request = alidns_models.UpdateDomainRecordRequest()
                update_domain_record_request.record_id = domain.record_id
                update_domain_record_request.rr = rr
                update_domain_record_request.type = 'A'
                update_domain_record_request.value = ip
                result = client.update_domain_record(update_domain_record_request)
                # print('更新结果', result.body)
        # print('等待', wait_time, '秒')
        time.sleep(wait_time)

    @staticmethod
    def get_ip():
        html_data = requests.get('http://www.net.cn/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.1
lxml==4.7.1
alibabacloud_tea_openapi==0.3.1
alibabacloud_alidns20150109==2.0.2

GitHub:https://github.com/weiangongsi/aliyun-ddns

防火布

你可能感兴趣的:(智能家居,python,阿里云)