最近有一個需求,須要把aliyun域名解析功能解析到本身到平臺上,其中總結了最主要的功能是(添加解析,修改解析,刪除解析,暫停解析,開啓解析等等)。因此下面的腳本主要實現這幾個功能,還有其餘功能本身能夠查資料擴展,也是很簡單的。python
使用步驟json
一、安裝 python2.7 (必須)api
二、安裝阿里雲api的python sdkbash
pip install aliyun-python-sdk-httpdns==2.1.0 pip install aliyun-python-sdk-domain pip install aliyun-python-sdk-alidns
三、阿里雲控制檯申請 "Access Key" ,並修改腳本中的 "ID" 與 "Secret"運維
四、更改腳本里面的對應信息:(access_id,access_secret,regiond(cn-shenzhen),DomainName,hostname,Types,IP等等)
dom
五、如下是dns api的腳本:python2.7
(env1) ➜ cat aliyun_dns.py #!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = 'chenmingle' 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 # 建立AcsClient實例 client = AcsClient( "access_id", "access_secret", "regiond(cn-shenzhen)" ); #獲取二級域名 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, IP): 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(IP) UpdateDomainRecordJson = json.loads(client.do_action_with_exception(UpdateDomainRecord)) print UpdateDomainRecordJson #增長域名解析 def add_dns_record(DomainName, hostname, Types, IP): AddDomainRecord = AddDomainRecordRequest.AddDomainRecordRequest() AddDomainRecord.set_DomainName(DomainName) AddDomainRecord.set_RR(hostname) AddDomainRecord.set_Type(Types) AddDomainRecord.set_TTL('600') AddDomainRecord.set_Value(IP) 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 DomainName = sys.argv for i in DomainName: if i == sys.argv[0]: pass else: list_dns_record(i) #edit_dns_record('test.xin', 'test', 'test_ok', 'A', '120.*.*.123') #add_dns_record('test.xin', 'test', 'A', '120.*.*.123') #delete_dns_record('test.xin','test_ok') #set_dns_record('test.xin', 'test_ok', 'DISABLE') #set_dns_record('test.xin', 'test_ok', 'ENABLE') list_domain()
最後測試下增長一個test的解析,而後改爲test_ok,以後暫停test_ok解析,開啓解析,最後刪除解析的流程:ide
增長解析的操做:測試
阿里雲上的解析:
阿里雲
修改的操做:
阿里雲上的解析結果:
暫停與開啓操做:
最後測試刪除解析:
阿里雲上的解析已經沒有了。這裏就實現了平時解析的主要操做,可是還有一些其餘操做能夠本身擴展下,最終能夠本身接到運維平臺上那樣就不須要運維過多的知道阿里雲帳號,並且在運維平臺上加上操做日誌,以防改錯後有一個跟蹤信息。