利用python實現修改阿里雲DNS值解析

參考文檔:html

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

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

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

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.yamlspa

# 阿里雲更新域名解析文檔
# 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 須要解析的地址 便可code

>python main.py 192.168.1.101
更新域名解析成功
域名:****** 主機:****** 記錄類型:A  記錄值:192.168.1.101

>
相關文章
相關標籤/搜索