-
Notifications
You must be signed in to change notification settings - Fork 0
/
change_ip.py
243 lines (214 loc) · 9.95 KB
/
change_ip.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 腾讯 云解析DNS API
# API文档 https://cloud.tencent.com/document/api/302/4032
import hashlib
import requests
import hmac
import random
import time
import base64
import json
import pandas as pd
class Sign(object):
"""
请求签名
"""
def __init__(self, secretKey):
self.secretKey = secretKey
def make(self, requestHost, requestUri, params, method='GET'):
"""
生成签名串
"""
srcStr = method.upper() + requestHost + requestUri + '?' + "&".join(
k.replace("_", ".") + "=" + str(params[k]) for k in sorted(params.keys()))
hashed = hmac.new(bytes(self.secretKey, 'utf8'), bytes(srcStr, 'utf8'), hashlib.sha1)
return base64.b64encode(hashed.digest())
class DnsHelper(object):
"""
DNS 相关操作类
## API密钥管理 https://console.cloud.tencent.com/cam/capi
"""
SecretId = '*********************'
SecretKey = ''*********************''
requestHost = 'cns.api.qcloud.com'
requestUri = '/v2/index.php'
def __init__(self):
self.params = {
'Timestamp': int(time.time()),
'SecretId': self.SecretId,
'Nonce': random.randint(10000000, 99999999),
}
self.url = 'https://%s%s' % (DnsHelper.requestHost, DnsHelper.requestUri)
def get_domain_list(self, offset=0, length=20, keyword='', qProjectId=''):
"""
获取域名列表
:param offset: 偏移量,默认为0。关于offset的更进一步介绍参考 API 简介中的相关小节。
:param length:返回数量,默认 20,最大值 100。关于limit的更进一步介绍参考 API 简介中的相关小节。
:param keyword:(过滤条件)根据关键字搜索域名
:param qProjectId:(过滤条件)项目ID
:return: 域名列表
"""
self.params['Action'] = 'DomainList'
self.params['offset'] = offset
self.params['length'] = length
self.params['keyword'] = keyword
if qProjectId:
self.params['qProjectId'] = qProjectId
self.params['Signature'] = Sign(self.SecretKey).make(self.requestHost, self.requestUri, self.params)
ret = requests.get(self.url, params=self.params)
return json.loads(ret.text)
def add_domain(self, domain, projectId=''):
"""
添加域名
:param domain:要添加的域名(主域名,不包括 www,例如:qcloud.com)
:param projectId:项目ID,如果不填写,则为“默认项目”
:return:
"""
self.params['Action'] = 'DomainCreate'
self.params['domain'] = domain
if projectId:
self.params['projectId'] = projectId
self.params['Signature'] = Sign(self.SecretKey).make(self.requestHost, self.requestUri, self.params)
ret = requests.get(self.url, params=self.params)
return json.loads(ret.text)
def set_domain_status(self, domain, status):
"""
设置域名状态
:param domain:要操作的域名(主域名,不包括 www,例如:qcloud.com)
:param status:可选值为:'disable' 和 'enable',分别代表暂停、启用
:return:
"""
self.params['Action'] = 'SetDomainStatus'
self.params['domain'] = domain
self.params['status'] = status
self.params['Signature'] = Sign(self.SecretKey).make(self.requestHost, self.requestUri, self.params)
ret = requests.get(self.url, params=self.params)
return json.loads(ret.text)
def delete_domain(self, domain):
"""
删除域名
:param domain: 要删除的域名
:return:
"""
self.params['Action'] = 'DomainDelete'
self.params['domain'] = domain
self.params['Signature'] = Sign(self.SecretKey).make(self.requestHost, self.requestUri, self.params)
ret = requests.get(self.url, params=self.params)
return json.loads(ret.text)
def get_record_list(self, domain_name, offset=0, length=20, subDomain='', recordType='', qProjectId=0):
"""
获取解析记录列表
:param domain_name: 要操作的域名(主域名,不包括 www,例如:qcloud.com)
:param offset:int类型,偏移量,默认为0。关于offset的更进一步介绍参考 API 简介中的相关小节。
:param length:int类型,返回数量,默认 20,最大值 100。关于limit的更进一步介绍参考 API 简介中的相关小节。
:param subDomain:(过滤条件)根据子域名进行过滤
:param recordType:(过滤条件)根据记录类型进行过滤
:param qProjectId:(过滤条件)项目ID, int类型
:return:
"""
self.params['Action'] = 'RecordList'
self.params['domain'] = domain_name
self.params['offset'] = offset
self.params['length'] = length
self.params['subDomain'] = subDomain
self.params['recordType'] = recordType
if qProjectId:
self.params['qProjectId'] = qProjectId
self.params['Signature'] = Sign(self.SecretKey).make(self.requestHost, self.requestUri, self.params)
ret = requests.get(self.url, params=self.params)
return json.loads(ret.text)
def add_record(self, domain, subDomain, recordType, value, recordLine='默认', ttl=600, mx=1):
"""
添加解析记录
:param domain:要添加解析记录的域名(主域名,不包括 www,例如:qcloud.com)
:param subDomain:子域名,例如:www
:param recordType:记录类型,可选的记录类型为:"A", "CNAME", "MX", "TXT", "NS", "AAAA", "SRV"
:param value:记录值, 如 IP:192.168.10.2, CNAME: cname.dnspod.com., MX: mail.dnspod.com.
:param recordLine:记录的线路名称,如:"默认"
:param ttl:TTL 值,范围1-604800,不同等级域名最小值不同,默认为 600
:param mx:MX优先级,范围为 0~50,当记录类型为 MX 时必选
:return:
"""
self.params['Action'] = 'RecordCreate'
self.params['domain'] = domain
self.params['subDomain'] = subDomain
self.params['recordType'] = recordType
self.params['value'] = value
self.params['recordLine'] = recordLine
self.params['ttl'] = ttl
if recordType.upper() == 'MX':
self.params['mx'] = mx
self.params['Signature'] = Sign(self.SecretKey).make(self.requestHost, self.requestUri, self.params)
ret = requests.get(self.url, params=self.params)
return json.loads(ret.text)
def update_record_status(self, domain, recordId, status):
"""
设置解析记录状态
:param domain:解析记录所在的域名
:param recordId:解析记录ID,int类型
:param status:可选值为:'disable' 和 'enable',分别代表暂停、启用
:return:
"""
self.params['Action'] = 'RecordStatus'
self.params['domain'] = domain
self.params['recordId'] = recordId
self.params['status'] = status
self.params['Signature'] = Sign(self.SecretKey).make(self.requestHost, self.requestUri, self.params)
ret = requests.get(self.url, params=self.params)
return json.loads(ret.text)
def update_record(self, domain, recordId, subDomain, recordType, value, recordLine='默认', ttl=600, mx=1):
"""
修改解析记录
:param domain: 要操作的域名(主域名,不包括 www,例如:qcloud.com)
:param recordId: int类型,解析记录的ID,可通过RecordList接口返回值中的 id 获取
:param subDomain: 子域名,例如:www
:param recordType: 记录类型,可选的记录类型为:"A", "CNAME", "MX", "TXT", "NS", "AAAA", "SRV"
:param value:记录值, 如 IP:192.168.10.2, CNAME: cname.dnspod.com., MX: mail.dnspod.com.
:param recordLine: 记录的线路名称,如:"默认"
:param ttl: TTL 值,范围1-604800,不同等级域名最小值不同,默认为 600, int类型
:param mx: MX优先级,范围为 0~50,当记录类型为 MX 时必选, int类型
:return:
"""
self.params['Action'] = 'RecordModify'
self.params['domain'] = domain
self.params['recordId'] = recordId
self.params['subDomain'] = subDomain
self.params['recordType'] = recordType
self.params['value'] = value
self.params['recordLine'] = recordLine
self.params['ttl'] = ttl
if recordType.upper() == 'MX':
self.params['mx'] = mx
self.params['Signature'] = Sign(self.SecretKey).make(self.requestHost, self.requestUri, self.params)
ret = requests.get(self.url, params=self.params)
return json.loads(ret.text)
def delete_record(self, domain, recordId):
"""
删除解析记录
:param domain:解析记录所在的域名
:param recordId:int类型,解析记录ID
:return:
"""
self.params['Action'] = 'RecordDelete'
self.params['domain'] = domain
self.params['recordId'] = recordId
self.params['Signature'] = Sign(self.SecretKey).make(self.requestHost, self.requestUri, self.params)
ret = requests.get(self.url, params=self.params)
return json.loads(ret.text)
def changeip():
try:
records = DnsHelper().get_record_list('example.local', 0, 100)
pc_value = records['data']['records']
d = pd.DataFrame(pc_value)
value = d.loc[d['name'] == 'pc', 'value'].values[0]
ip = requests.get("http://icanhazip.com").content.decode()
print('tencent record:{} , net ip:{}'.format(value, ip))
if value != ip:
a_record = {'domain': 'example.local', 'recordId': 432046253, 'value': ip, 'subDomain': 'pc',
'recordType': 'A'}
print(DnsHelper().update_record(**a_record))
except Exception as ex:
print(ex)
if __name__ == '__main__':
changeip()