forked from X-Mars/UpgradeDNS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alidns-python3.py
77 lines (64 loc) · 2.18 KB
/
alidns-python3.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
#!/usr/bin/python3
#dependence:
'''
pip3 install aliyun-python-sdk-core-v3 (2.5.2)
pip3 install aliyun-python-sdk-alidns (2.0.7)
'''
#aliyunsdk:
from aliyunsdkcore import client
from aliyunsdkalidns.request.v20150109 import DescribeDomainRecordsRequest
from aliyunsdkalidns.request.v20150109 import UpdateDomainRecordRequest
#else:
from argparse import ArgumentParser
import json
#parser_arguments:
def main():
parser = ArgumentParser()
parser.add_argument('AccessKeyId')
parser.add_argument('AccessKeySecret')
parser.add_argument('Domain')
parser.add_argument('-q' ,action = 'store_true', help = 'query domain records.')
parser.add_argument('-u' ,metavar = '<ip>', help = 'update domain ip.')
args = parser.parse_args()
print(run(args.AccessKeyId, args.AccessKeySecret, args.Domain, args.u, args.q))
#module run:
def run(access_key_id, access_key_secret, domain, update = None, query = None):
bs = client.AcsClient(access_key_id, access_key_secret, 'cn-hangzhou')
records = query_record(bs, domain)
if update:
if records['@'][0] == update:
return True
return update_record(bs, records['@'][3], update)
if query:
return records
return records['@'][0]
def update_record(bs, record_id, ip):
ud = UpdateDomainRecordRequest.UpdateDomainRecordRequest()
ud.set_accept_format('json')
ud.set_RecordId(record_id)
ud.set_RR('@')
ud.set_Type('A')
ud.set_TTL('600')
ud.set_Value(ip)
js = json.loads(bs.do_action_with_exception(ud).decode())
#print(js)
if js['RecordId'] == record_id:
return True
return False
#query record return a dictionary
def query_record(bs, domain):
q = DescribeDomainRecordsRequest.DescribeDomainRecordsRequest()
q.set_accept_format('json')
q.set_DomainName(domain)
js = json.loads(bs.do_action_with_exception(q).decode())
ret = {}
for x in js['DomainRecords']['Record']:
RR = x['RR']
Type = x['Type']
Value = x['Value']
RecordId = x['RecordId']
TTL = x['TTL']
ret[RR] = [Value, Type, TTL, RecordId]
return ret
if __name__ == '__main__':
main()