-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaliyunsdk.py
80 lines (68 loc) · 2.38 KB
/
aliyunsdk.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
#!/usr/bin/python
# -*- coding:utf8 -*-
# author: colbrze430@gmail.com
# date: 2018.05.31
# 调用 阿里云SDK
'''
https://help.aliyun.com/document_detail/53090.html?spm=5176.11122631.962077.5.22722a6auD6fAk
1. 安装 SDK
python2:
pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-ecs
python3:
pip install aliyun-python-sdk-core-v3
pip install aliyun-python-sdk-ecs
域名 SDK:
pip install aliyun-python-sdk-domain (误)
pip install aliyun-python-sdk-alidns
2. 获取 Region ID、AccessKey ID和AccessKey Secret
- [Region ID](https://www.alibabacloud.com/help/zh/doc-detail/40654.htm)
'''
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkalidns.request.v20150109 import DescribeDomainRecordsRequest
from aliyunsdkalidns.request.v20150109 import UpdateDomainRecordRequest
import json
class DomainTool:
def __init__(self, client, domain):
self.client = client
self.domain = domain
self.records = []
def get_records(self):
r = DescribeDomainRecordsRequest.DescribeDomainRecordsRequest()
r.set_DomainName(self.domain)
res = self.client.do_action_with_exception(r)
records = json.loads(res.decode())['DomainRecords']['Record']
for record in records:
if record['Type'] == 'A':
self.records.append(record)
def get_ip(self):
if not self.records:
self.get_records()
ip = self.records[0]['Value']
print(ip)
return ip
def change_ip(self, ip):
if not self.records:
self.get_records()
for record in self.records:
r = UpdateDomainRecordRequest.UpdateDomainRecordRequest()
r.set_RecordId(record['RecordId'])
r.set_RR(record['RR'])
r.set_Type(record['Type'])
r.set_Value(ip)
try:
res = self.client.do_action_with_exception(r)
print(res)
except Exception as e:
print(e)
raise
if __name__ == '__main__':
from aliyunconfig import Config
client = AcsClient(Config.AccessKeyId, Config.Secret)
c = DomainTool(client, Config.Domain)
print(c.domain)
print(c.get_ip())
c.change_ip('1.1.1.1')
print(c.get_ip())