forked from X-Mars/UpgradeDNS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpgradeDNS.py
67 lines (55 loc) · 2.09 KB
/
UpgradeDNS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#coding:utf-8
#Auther:火星小刘
#Email:[email protected]
#转载请保留出处
from aliyunsdkcore import client
from aliyunsdkalidns.request.v20150109 import DescribeDomainsRequest,DescribeDomainRecordsRequest,UpdateDomainRecordRequest
import json,urllib,re
#替换以下参数
ID="xxxxx"
Secret="xxxxx"
RegionId="cn-hangzhou"
DomainName="xxxx.com"
#想要自动修改的主机名和域名类型
HostNameList = ['www','@']
Types = "A"
clt = client.AcsClient(ID,Secret,RegionId)
#获取公网ip
def GetLocalIP():
IPInfo = urllib.urlopen("https://api.ipify.org/?format=json").read()
IP = IPInfo['ip']
return IP
#获取域名列表(暂时无用)
def GetDomainList():
DomainList = DescribeDomainsRequest.DescribeDomainsRequest()
DomainList.set_accept_format('json')
DNSListJson = json.loads(clt.do_action_with_exception(DomainList))
print DNSListJson
#更新域名ip
def EditDomainRecord(HostName, RecordId, Types, IP):
UpdateDomainRecord = UpdateDomainRecordRequest.UpdateDomainRecordRequest()
UpdateDomainRecord.set_accept_format('json')
UpdateDomainRecord.set_RecordId(RecordId)
UpdateDomainRecord.set_RR(HostName)
UpdateDomainRecord.set_Type(Types)
UpdateDomainRecord.set_TTL('600')
UpdateDomainRecord.set_Value(IP)
UpdateDomainRecordJson = json.loads(clt.do_action_with_exception(UpdateDomainRecord))
print UpdateDomainRecordJson
#获取域名信息
def GetAllDomainRecords(DomainName, Types, IP):
DomainRecords = DescribeDomainRecordsRequest.DescribeDomainRecordsRequest()
DomainRecords.set_accept_format('json')
DomainRecords.set_DomainName(DomainName)
DomainRecordsJson = json.loads(clt.do_action_with_exception(DomainRecords))
for HostName in HostNameList:
for x in DomainRecordsJson['DomainRecords']['Record']:
RR = x['RR']
Type = x['Type']
if RR == HostName and Type == Types:
RecordId = x['RecordId']
print RecordId
EditDomainRecord(HostName, RecordId, Types, IP)
IP = GetLocalIP()
GetDomainList()
GetAllDomainRecords(DomainName, Types, IP)