• 利用python实现修改阿里云DNS值解析


    参考文档:

    https://help.aliyun.com/document_detail/29776.html

    https://help.aliyun.com/document_detail/29774.html

    https://help.aliyun.com/knowledge_detail/38738.html

    Git地址: https://gitee.com/wang_li/AutoUpdateAliDnsRecord.git

    Python代码
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    import json
    import yaml
    import sys
    from aliyunsdkcore.client import AcsClient
    from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import DescribeDomainRecordsRequest
    from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest
    
    def AliAccessKey(id,Secret,region):
        try:
            client = AcsClient(id, Secret, region)
            return client
        except Exception as e:
            print("验证aliyun key失败")
            print(e)
            sys.exit(-1)
    
    def read_yaml(filename):
        try:
            yaml_file = open(filename,"rb")
            yaml_data = yaml.safe_load(yaml_file)
            yaml_file.close()
            return yaml_data
        except Exception as e:
            print("读取配置文件错误")
            print(e)
            sys.exit(-1)
    
    def GetDNSRecordId(yaml_data,client,DomainName):
        try:
            request = DescribeDomainRecordsRequest()
            request.set_accept_format('json')
            request.set_DomainName(DomainName)
            response = client.do_action_with_exception(request)
            json_data = json.loads(str(response, encoding='utf-8'))
    
            for RecordId in json_data['DomainRecords']['Record']:
                if yaml_data['UserData']['RR'] == RecordId['RR']:
                    return RecordId['RecordId']
    
        except Exception as e:
            print("获取RecordId失败")
            print(e)
            sys.exit(-1)
    
    def UpdateDomainRecord(client,yaml_data,RecordId):
        try:
            request = UpdateDomainRecordRequest()
            request.set_accept_format('json')
    
            if 'Auto_Lines' == yaml_data['UserData']['UpdateDomain']:
                DomainValue = sys.argv[1]
            else :
                DomainValue = yaml_data['UserData']['UpdateDomain']
    
            request.set_Value(DomainValue)
            request.set_Type(yaml_data['UserData']['DomainType'])
            request.set_RR(yaml_data['UserData']['RR'])
            request.set_RecordId(RecordId)
            response = client.do_action_with_exception(request)
            print("更新域名解析成功")
            print("域名:" + yaml_data['UserData']['DomainName'] + " 主机:" + yaml_data['UserData']['RR'] + " 记录类型:" +  yaml_data['UserData']['DomainType'] + " 记录值:" +  DomainValue)
        except Exception as e:
            print("更新域名解析失败")
            print(e)
    
    
    def main():
        yaml_data = read_yaml('conf/conf.yaml')
        client = AliAccessKey(yaml_data['AliyunData']['AccessKey_ID'],yaml_data['AliyunData']['Access_Key_Secret'],yaml_data['AliyunData']['region_id'])
        RecordId = GetDNSRecordId(yaml_data,client,yaml_data['UserData']['DomainName'])
        UpdateDomainRecord(client,yaml_data,RecordId)
    
    if __name__ == "__main__" :
        main()
    安装依赖
    pip3 install pyyaml
    pip3 install aliyun-python-sdk-core-v3
    pip3 install aliyun-python-sdk-alidns==2.0.6
    配置文件介绍

    所在路径: ./conf/conf.yaml

    # 阿里云更新域名解析文档
    # https://help.aliyun.com/document_detail/29776.html
    # https://help.aliyun.com/document_detail/29774.html
    
    
    # 阿里云信息,从阿里云获取填写即可
    AliyunData:
      # 阿里云的AccessKey_ID , Access_Key_Secret , region_id
      # 获取方法,参考文档: https://help.aliyun.com/knowledge_detail/38738.html
      AccessKey_ID: 'AccessKey'
      Access_Key_Secret: 'Secret'
      region_id: "cn-hangzhou"
    
    # DNS解析信息
    UserData:
      # 需要修改的域名
      DomainName: '需要修改的域名'
      # 解析的主机
      RR: '需要修改的主机'
      # 解析的记录
      DomainType: '需要修改的记录类型'
      # 解析更新的值,如果填写 Auto_Lines ,则从命令行获取
      UpdateDomain: 'Auto_Lines'
      #UpdateDomain: '127.0.0.1'
    根据配置文档来解析

    需要将配置文件中的UpdateDomain 修改为需要修改解析的值,执行main.py即可

    >python main.py
    更新域名解析成功
    域名:****** 主机:****** 记录类型:A 记录值:192.168.1.100
    
    >
    根据用户命令行指定来解析

    需要将配置文件中的UpdateDomain 修改为Auto_Lines,执行main.py 需要解析的地址 即可

    >python main.py 192.168.1.101
    更新域名解析成功
    域名:****** 主机:****** 记录类型:A  记录值:192.168.1.101
    
    >
    欢迎转发! 请保留源地址: https://www.cnblogs.com/NoneID
  • 相关阅读:
    hdu 6435 Problem J. CSGO 最长曼哈顿距离+二进制枚举
    UVA11419 SAM I AM 二分图最小点覆盖
    数论入门基础整理
    [算法导论]#1 摊还分析
    [转载]markown语法
    [AC自动机]玄武密码
    [学习笔记]AC自动机
    分火腿
    [学习笔记]二分与分治
    [学习笔记]二项式反演
  • 原文地址:https://www.cnblogs.com/NoneID/p/10960292.html
Copyright © 2020-2023  润新知