-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdatedns.py
executable file
·108 lines (73 loc) · 2.8 KB
/
updatedns.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# coding=UTF-8
#!/usr/bin/env python
from getip import GetPublicIp
import time,method,os
DNSURL = "http://alidns.aliyuncs.com"
#https://develop.aliyun.com/api?spm=5176.doc29821.2.6.xsUCfs 从此页面进入点击创建secretID\KEY
Access_Key_ID = "Access_Key_IDAccess_Key_IDAccess_Key_ID"
Access_Key_Secret = "Access_Key_SecretAccess_Key_Secret"
updateRecord = ['www','*']
Version = "2015-01-09"
DomainName = "xxxxx.com"
SignatureMethod = "HMAC-SHA1"
SignatureVersion = "1.0"
Format = "JSON"
#公式参数:
parameters = {}
parameters["Action"] = ""
parameters["DomainName"] = DomainName
parameters["Version"] = Version
parameters["AccessKeyId"] = Access_Key_ID
parameters["Timestamp"] = ""
parameters["SignatureNonce"] = ""
parameters["SignatureMethod"] = SignatureMethod
parameters["SignatureVersion"] = SignatureVersion
parameters["Format"] = Format
IPFILEPATH = os.path.expanduser('~') + "/ip"
while 1:
try:
ipFileR = open(IPFILEPATH, 'r')
localIP = ipFileR.readline()
ipFileR.close()
except:
localIP = '1'
#获取外网地址:
getmyip = GetPublicIp()
myip = getmyip.getip()
#如果文本记录中的IP跟当前IP不同,就得更新文件,更新DNS
if cmp(GetPublicIp.IP_ERROR,myip) !=0 and cmp(localIP,myip) != 0:
try:
#print 1
result = method.DescribeDomainRecords(DNSURL,Access_Key_Secret,parameters)
for record in result['DomainRecords']['Record']:
if record['RR'] in updateRecord:
method.UpdateDomainRecord(DNSURL, Access_Key_Secret, parameters,record, myip)
time.sleep(1)
# print 2
#print method.UpdateDomainRecord(DNSURL,Access_Key_Secret,parameters,result['DomainRecords']['Record'][0],myip)
#要等待一秒后再次请求,要不然90%出错
#time.sleep(1)
# print 3
#print method.UpdateDomainRecord(DNSURL,Access_Key_Secret,parameters,result['DomainRecords']['Record'][1],myip)
#print 4
ipFileW = open(IPFILEPATH, 'w')
ipFileW.write(myip)
ipFileW.close()
except Exception as e:
print(e)
time.sleep(60*10)
#方法参数:
# parameters["TypeKeyWord"] = "A"
#
# print(sign.getSign(parameters,Access_Key_Secret))
# print(sign.getURLWithPara(parameters))
# URL = DNSURL + "?Signature="+sign.getSign(parameters,Access_Key_Secret)+"&"+sign.getURLWithPara(parameters)
#
# opener = urllib2.urlopen(URL)
# str = opener.read()
# print(str)
# jsonR = json.loads(str)
# print jsonR['DomainRecords']['Record'][0]
# print jsonR['DomainRecords']['Record'][1]
# print(type(json.loads(str)))
# print(type(json.dumps(str)))