Python 官方文档:入门教程 => 点击学习
最近有一个需求,需要把aliyun域名解析功能解析到自己到平台上,其中总结了最主要的功能是(添加解析,修改解析,删除解析,暂停解析,开启解析等等)。所以下面的脚本主要实现这几个功能,还有其他功能自己可以查资料扩展,也是很简单的。使用步骤1、
最近有一个需求,需要把aliyun域名解析功能解析到自己到平台上,其中总结了最主要的功能是(添加解析,修改解析,删除解析,暂停解析,开启解析等等)。所以下面的脚本主要实现这几个功能,还有其他功能自己可以查资料扩展,也是很简单的。
使用步骤
1、安装 python2.7 (必须)
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解析,开启解析,最后删除解析的流程:
增加解析的操作:
阿里云上的解析:
修改的操作:
阿里云上的解析结果:
暂停与开启操作:
最后测试删除解析:
阿里云上的解析已经没有了。这里就实现了平时解析的主要操作,但是还有一些其他操作可以自己扩展下,最终可以自己接到运维平台上那样就不需要运维过多的知道阿里云账号,而且在运维平台上加上操作日志,以防改错后有一个跟踪信息。
--结束END--
本文标题: aliyun域名解析python api
本文链接: https://lsjlt.com/news/190544.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0