本文分享用Python操作阿里云域名API的脚本。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# http://blog.51cto.com/legehappy/2132932
# pip install aliyun-python-sdk-httpdns
# pip install aliyun-python-sdk-domain
# pip install aliyun-python-sdk-alidns
# pip install aliyun-python-sdk-ecs
# From https://github.com/chenmingle/aliyun-python-commonapi
# mod by https://vps123.top
from aliyunsdkcore.client import AcsClient
from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest
from aliyunsdkecs.request.v20140526 import StopInstanceRequest
from aliyunsdkalidns.request.v20150109 import DescribeDomainsRequest, DescribeDomainRecordsRequest, UpdateDomainRecordRequest, AddDomainRecordRequest, DeleteDomainRecordRequest, SetDomainRecordStatusRequest
import json, urllib, re
import sys
import argparse
# https://www.alibabacloud.com/help/zh/doc-detail/50086.htm
# "LocalName":"华南 1",
# "RegionId":"cn-shenzhen"
# "LocalName":"亚太东南 1 (新加坡)",
# "RegionId":"ap-southeast-1"
# "LocalName":"华北 1",
# "RegionId":"cn-qingdao"
# "LocalName":"华北 2",
# "RegionId":"cn-beijing"
# "LocalName":"华东 2",
# "RegionId":"cn-shanghai"
# "LocalName":"美国东部 1 (弗吉尼亚)",
# "RegionId":"us-east-1"
# "LocalName":"香港",
# "RegionId":"cn-hongkong"
# "LocalName":"中东东部 1 (迪拜)",
# "RegionId":"me-east-1"
# "LocalName":"亚太东南 2 (悉尼)",
# "RegionId":"ap-southeast-2"
# "LocalName":"华东 1",
# "RegionId":"cn-hangzhou"
# "LocalName":"欧洲中部 1 (法兰克福)",
# "RegionId":"eu-central-1"
# "LocalName":"亚太东北 1 (东京)",
# "RegionId":"ap-northeast-1"
# "LocalName":"美国西部 1 (硅谷)",
# "RegionId":"us-west-1"
# 创建AcsClient实例
client = AcsClient(
"XLe6gHSQYEuaT8Fb",
"Ln5j1u6Fu3JDUFvlrr7YqVv37fh5ma",
"cn-qingdao"
);
#获取二级域名
def list_domain():
DomainList = DescribeDomainsRequest.DescribeDomainsRequest()
DomainList.set_accept_format('json')
DNSListJson = json.loads(client.do_action_with_exception(DomainList))
for i in DNSListJson['Domains']['Domain']:
print i['DomainName']
# print DNSListJson
#获取域名信息(修改自己的DomainName)
def list_dns_record(DomainName):
DomainRecords = DescribeDomainRecordsRequest.DescribeDomainRecordsRequest()
DomainRecords.set_accept_format('json')
DomainRecords.set_DomainName(DomainName)
DomainRecordsJson = json.loads(client.do_action_with_exception(DomainRecords))
print DomainName+':'
# print DomainRecordsJson
for x in DomainRecordsJson['DomainRecords']['Record']:
RecordId = x['RecordId']
RR = x['RR']
Type = x['Type']
Line = x['Line']
Value = x['Value']
TTL = x['TTL']
Status = x['Status']
txt = RecordId+' '+RR+' '+Type+' '+Line+' '+Value+' '+str(TTL)+' '+Status
print txt
print '\n'
#更新域名解析(修改的域名DomainName,和要修改的hostname,修改成hostname_new,解析的类型Types,解析的地址IP)
def edit_dns_record(DomainName, hostname, hostname_new, Types, RecordValue):
DomainRecords = DescribeDomainRecordsRequest.DescribeDomainRecordsRequest()
DomainRecords.set_accept_format('json')
DomainRecords.set_DomainName(DomainName)
DomainRecordsJson = json.loads(client.do_action_with_exception(DomainRecords))
for x in DomainRecordsJson['DomainRecords']['Record']:
if hostname == x['RR']:
RecordId = x['RecordId']
UpdateDomainRecord = UpdateDomainRecordRequest.UpdateDomainRecordRequest()
UpdateDomainRecord.set_accept_format('json')
UpdateDomainRecord.set_RecordId(RecordId)
UpdateDomainRecord.set_RR(hostname_new)
UpdateDomainRecord.set_Type(Types)
UpdateDomainRecord.set_TTL('600')
UpdateDomainRecord.set_Value(RecordValue)
UpdateDomainRecordJson = json.loads(client.do_action_with_exception(UpdateDomainRecord))
print UpdateDomainRecordJson
#增加域名解析
def add_dns_record(DomainName, hostname, Types, RecordValue):
AddDomainRecord = AddDomainRecordRequest.AddDomainRecordRequest()
AddDomainRecord.set_DomainName(DomainName)
AddDomainRecord.set_RR(hostname)
AddDomainRecord.set_Type(Types)
AddDomainRecord.set_TTL('600')
AddDomainRecord.set_Value(RecordValue)
AddDomainRecordJson = json.loads(client.do_action_with_exception(AddDomainRecord))
print AddDomainRecordJson
#删除域名解析
def delete_dns_record(DomainName, hostname):
DomainRecords = DescribeDomainRecordsRequest.DescribeDomainRecordsRequest()
DomainRecords.set_accept_format('json')
DomainRecords.set_DomainName(DomainName)
DomainRecordsJson = json.loads(client.do_action_with_exception(DomainRecords))
for x in DomainRecordsJson['DomainRecords']['Record']:
if hostname == x['RR']:
RecordId = x['RecordId']
DeleteDomainRecord = DeleteDomainRecordRequest.DeleteDomainRecordRequest()
DeleteDomainRecord.set_RecordId(RecordId)
DeleteDomainRecordJson = json.loads(client.do_action_with_exception(DeleteDomainRecord))
print DeleteDomainRecordJson
#设置域名解析
def set_dns_record(DomainName, hostname, status):
DomainRecords = DescribeDomainRecordsRequest.DescribeDomainRecordsRequest()
DomainRecords.set_accept_format('json')
DomainRecords.set_DomainName(DomainName)
DomainRecordsJson = json.loads(client.do_action_with_exception(DomainRecords))
for x in DomainRecordsJson['DomainRecords']['Record']:
if hostname == x['RR']:
RecordId = x['RecordId']
SetDomainRecord = SetDomainRecordStatusRequest.SetDomainRecordStatusRequest()
SetDomainRecord.set_accept_format('json')
SetDomainRecord.set_RecordId(RecordId)
SetDomainRecord.set_Status(status)
SetDomainRecordJson = json.loads(client.do_action_with_exception(SetDomainRecord))
print SetDomainRecordJson
def update_dns_cert_record(DomainName, RecordValue):
edit_dns_record(DomainName, "_acme-challenge", "_acme-challenge", "TXT", RecordValue)
def main(args):
if args.mode == "list":
list_domain()
elif args.mode == "get":
for i in args.domain.split(","):
list_dns_record(i)
elif args.mode == "add":
add_dns_record(args.domain, args.hostname, args.recordType, args.value)
elif args.mode == "update":
edit_dns_record(args.domain, args.hostname, args.hostname, args.recordType, args.value)
elif args.mode == "delete":
delete_dns_record(args.domain, args.hostname)
elif args.mode == "cert":
update_dns_cert_record(args.domain, args.value)
if __name__ == '__main__':
argParser = argparse.ArgumentParser()
argParser.add_argument('-m', '--mode', dest='mode', type=str, help='mode', required=True, choices=['list', 'get', 'add', 'update', 'delete', 'cert'])
argParser.add_argument('-d', '--domain', dest='domain', type=str, help='domain')
argParser.add_argument('-n', '--hostname', dest='hostname', type=str, help='hostname')
argParser.add_argument('-v', '--value', dest='value', type=str, help='value')
argParser.add_argument('-t', '--recordType', dest='recordType', type=str, help='recordType')
args = argParser.parse_args()
main(args)
-- EOF --
本文最后修改于6年前 (2019-06-04)