1. 程式人生 > >aliyun域名解析python api

aliyun域名解析python api

雲上 測試 ads TP proc 加域 accept lba import

最近有一個需求,需要把aliyun域名解析功能解析到自己到平臺上,其中總結了最主要的功能是(添加解析,修改解析,刪除解析,暫停解析,開啟解析等等)。所以下面的腳本主要實現這幾個功能,還有其他功能自己可以查資料擴展,也是很簡單的。


使用步驟

1、安裝 python2.7 (必須)

2、安裝阿裏雲api的python sdk

 pip install aliyun-python-sdk-httpdns==2.1.0
 pip install aliyun-python-sdk-domain
 pip install aliyun-python-sdk-alidns


3、阿裏雲控制臺申請 "Access Key"

,並修改腳本中的 "ID""Secret"

4、更改腳本裏面的對應信息:(access_id,access_secret,regiond(cn-shenzhen),DomainName,hostname,Types,IP等等)

5、以下是dns api的腳本:

(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解析,開啟解析,最後刪除解析的流程:

增加解析的操作:

技術分享圖片

阿裏雲上的解析:

技術分享圖片


修改的操作:

技術分享圖片

阿裏雲上的解析結果:

技術分享圖片


暫停與開啟操作:

技術分享圖片

技術分享圖片

最後測試刪除解析:

技術分享圖片

阿裏雲上的解析已經沒有了。這裏就實現了平時解析的主要操作,但是還有一些其他操作可以自己擴展下,最終可以自己接到運維平臺上那樣就不需要運維過多的知道阿裏雲賬號,而且在運維平臺上加上操作日誌,以防改錯後有一個跟蹤信息。

aliyun域名解析python api