阿里云ddns ipv6解析程序
阿里云ddns ipv6解析程序

阿里云ddns ipv6解析程序

阿里云ddns ipv6解析程序

由于网上很多资源没有更新,一些接口阿里云方面已经调整,所以,结合这次自己部署经验,记录如下:
程序没有新增,如果没有记录,请手动添加一条。

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import sys

from typing import List
from Tea.core import TeaCore
from alibabacloud_alidns20150109.client import Client as Alidns20150109Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_alidns20150109 import models as alidns_20150109_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient
from urllib.request import urlopen

class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client(
        access_key_id: str,
        access_key_secret: str,
    ) -> Alidns20150109Client:
        """
        使用AK&SK初始化账号Client
        @param access_key_id:
        @param access_key_secret:
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config(
            # 必填,您的 AccessKey ID,
            access_key_id=access_key_id,
            # 必填,您的 AccessKey Secret,
            access_key_secret=access_key_secret
        )
        # 访问的域名
        config.endpoint = f'alidns.cn-hangzhou.aliyuncs.com'
        return Alidns20150109Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        client = Sample.create_client('key', 'scret') #填写自己的key和密钥,在阿里云申请ram调试账号
        runtime = util_models.RuntimeOptions()
        try:
            ip = urlopen('https://api-ipv6.ip.sb/ip').read()  # 使用IP.SB的接口获取ipv6地址
            ipv6 = str(ip, encoding='utf-8')
            
                    #获取子域名的id信息
            describe_sub_domain_records_request = alidns_20150109_models.DescribeSubDomainRecordsRequest(
                    sub_domain='ipv6.zhangdong.site',
                    type='AAAA'
                ) 
            # 复制代码运行请自行打印 API 的返回值
            resp = client.describe_sub_domain_records_with_options(describe_sub_domain_records_request, runtime)
            # res = UtilClient.to_jsonstring(TeaCore.to_map(resp))
            res = TeaCore.to_map(resp)
            print(res['statusCode'])
            if res['body']['TotalCount'] == 0:
                print("未查到AAAA记录,请新增")
            else:
                Record = res['body']['DomainRecords']['Record'][0]
                if Record['Value'] == ipv6.strip() :
                    print('IPv6地址没变无需更改')
                else:
                     #更新域名信息
                    update_domain_record_request = alidns_20150109_models.UpdateDomainRecordRequest( 
                        rr='ipv6',
                        type='AAAA',
                        value= ipv6,
                        record_id=Record['RecordId'])
                    resp= client.update_domain_record_with_options(update_domain_record_request, runtime)
                    res2 = TeaCore.to_map(resp)
                    if res2['statusCode'] == 200:
                        print("修改域名解析成功")
                    else:
                        print(res2)
        except Exception as error:
            # 如有需要,请打印 error
            UtilClient.assert_as_string(error.message)


if __name__ == '__main__':
    Sample.main(sys.argv[1:])

接着添加一个定时任务即可