-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDDNS.py
102 lines (91 loc) · 3.55 KB
/
DDNS.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
import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.dnspod.v20210323 import dnspod_client, models
import requests
import re
import time
def ReturnRecordId(Domain, SubDomain):
# 获取指定记录的RecordId
# Domain 主域名
# SubDomain 待修改的子域
try:
cred = credential.Credential(SecretId, SecretKey)
httpProfile = HttpProfile()
httpProfile.endpoint = "dnspod.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = dnspod_client.DnspodClient(cred, "", clientProfile)
req = models.DescribeRecordListRequest()
params = {
"Domain": Domain
}
req.from_json_string(json.dumps(params))
resp = client.DescribeRecordList(req)
for record in resp.RecordList:
if record.Name == SubDomain:
return record.RecordId
print("未找到对应的记录值,请先创建相应的主机记录!")
return -2
except TencentCloudSDKException as err:
print("获取域名的记录列表失败,请重试!")
return -1
def ModifyDynamicDNS(RecordId, Domain ,SubDomain, Ip):
# 动态域名解析API
# RecordId 待修改记录ID
# Domain 主域名
# SubDomain 子域名
try:
cred = credential.Credential(SecretId, SecretKey)
httpProfile = HttpProfile()
httpProfile.endpoint = "dnspod.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = dnspod_client.DnspodClient(cred, "", clientProfile)
req = models.ModifyDynamicDNSRequest()
params = {
"Domain": Domain,
"SubDomain": SubDomain,
"RecordId": RecordId,
"RecordLine": "默认",
"Value": Ip
}
req.from_json_string(json.dumps(params))
resp = client.ModifyDynamicDNS(req)
if str(RecordId) in resp.to_json_string():
print("更新成功!")
return 1
except TencentCloudSDKException as err:
return 0
def GetCurrentIP():
resp = requests.get('https://ip.tool.lu/').content
resp = resp.decode('utf8')
IPPattern = '\d+.\d+.\d+.\d+'
matchObj = re.search(IPPattern, resp)
return matchObj.group()
if __name__ == "__main__":
SecretId = ""
SecretKey = ""
Domain = "eatrice.cn" # 主域名
SubDomain = "homesource" # 指定要修改的子域名
interval = 600 # 每10分钟检查一次IP
RecordId = ReturnRecordId(Domain=Domain, SubDomain=SubDomain)
if RecordId == -1:
print("RecordList请求发生问题!")
exit()
if RecordId == -2:
print("没有找到你要的子域名,请先新建一个!")
OldIP = ""
while True:
CurrentIP = GetCurrentIP()
if OldIP != CurrentIP:
res = ModifyDynamicDNS(RecordId=RecordId, Domain=Domain, SubDomain=SubDomain, Ip = CurrentIP)
if res:
print(f'IP成功更新!原IP:{OldIP},新IP:{CurrentIP}')
OldIP = CurrentIP
else:
print('动态域名解析API出问题了,正在重试!')
continue
time.sleep(interval)