庆祝中华人民共和国成立70周年

高举旗帜 团结一致 锐意进取

使用aliyun-sdk开发的一个解析管理小工具

安装aliyunSDK核心库:
    python2:pip install aliyun-python-sdk-core
    python3:pip install aliyun-python-sdk-core-v3
安装aliyunSDK DNS产品库:
    pip install aliyun-python-sdk-alidns

运行效果:

《使用aliyun-sdk开发的一个解析管理小工具》

代码:

#!/usr/bin/env python
# coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import DescribeDomainRecordsRequest
from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest
from aliyunsdkalidns.request.v20150109.AddDomainRecordRequest import AddDomainRecordRequest

import json

accessKeyId = "<accessKeyId >"
accessSecret = "<accessSecret >"
domainName = "<domainName >"

client = AcsClient(accessKeyId, accessSecret, 'cn-hangzhou')


def main():
    request = DescribeDomainRecordsRequest()
    request.set_accept_format('json')
    request.set_DomainName(domainName)
    request.set_PageSize(100)
    request.set_Type("A")
    response = client.do_action_with_exception(request)
    # print(response)

    response = json.loads(response)
    print
    print("Only show A record".center(68))
    print("┌──────────────────────────────────────────────────────────────────┐")
    print("│ Key".ljust(5)),
    print("│  RR".ljust(15)),
    print("│  Value".ljust(25)),
    print("│  RecordId".ljust(25) + "│")
    records = response["DomainRecords"]["Record"]
    i = 0
    for record in records:
        i += 1
        print("├──────────────────────────────────────────────────────────────────┤")
        print("│ " + str(i).ljust(3)),
        print("│  " + str(record["RR"].ljust(10))),
        print("│  " + str(record["Value"].ljust(20))),
        print("│  " + str(record["RecordId"].ljust(20)) + "│")
    print("└──────────────────────────────────────────────────────────────────┘")

    try:
        key = int(raw_input("\033[1;33mTyping 'Key' to modify record or typing '0' to add:\033[0m"))
    except BaseException as e:
        print "\033[1;31m'Key' must be integer\033[0m"
        exit()

    if key:
        select_record = records[int(key) - 1]
        print "\033[1;34mSelected: " + select_record["RR"] + "." + select_record["DomainName"] + "\033[0m"
        RR = raw_input("\033[1;33mTyping new Record or ENTER:\033[0m")
        if RR:
            select_record["RR"] = RR

        Value = raw_input("\033[1;33mTyping new Value or ENTER:\033[0m")
        if Value:
            select_record["Value"] = Value

        try:
            request1 = UpdateDomainRecordRequest()
            request1.set_accept_format('json')
            request1.set_RecordId(select_record["RecordId"])
            request1.set_RR(select_record["RR"])
            request1.set_Type("A")
            request1.set_Value(select_record["Value"])
            response1 = client.do_action_with_exception(request1)
            print "RequestId: " + json.loads(response1)["RequestId"]
            print "\033[1;32mModify Successful!\033[0m"
        except ServerException as e:
            print "\033[1;31m" + e.get_error_msg() + "\033[0m"

    else:
        new_record = []
        RR = raw_input("\033[1;33mTyping new Record:\033[0m")
        Value = raw_input("\033[1;33mTyping new Value:\033[0m")

        if RR and Value:
            try:
                request2 = AddDomainRecordRequest()
                request2.set_accept_format('json')

                request2.set_DomainName(domainName)
                request2.set_RR(RR)
                request2.set_Type("A")
                request2.set_Value(Value)

                response2 = client.do_action_with_exception(request2)
                print "RequestId: " + json.loads(response2)["RequestId"]
                print "\033[1;32mAdd Successful!\033[0m"
            except ServerException as e:
                print "\033[1;31m" + e.get_error_msg() + "\033[0m"

        else:
            print "\033[1;31mRecord and Value is required\033[0m"


if __name__ == '__main__':
    main()

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注