Python 实现DDNS 动态解析到阿里

什么是 DDNS?

DDNS(Dynamic Domain Name Server,动态域名服务)是将用户的动态IP地址映射到一个固定的域名解析服务上,用户每次连接网络的时候客户端程序就会通过信息传递把该主机的动态IP地址传送给位于服务商主机上的服务器程序,服务器程序负责提供DNS服务并实现动态域名解析

DDNS的使用场景分析

在中国,面向社会的服务器是需要备案的,而备案的时候是需要使用一个固定的公网ip备案的。因此很多人想要使用DDNS实现网站搭建的小伙伴就要失望了(说的就是我)。

但是这里面有一个细节,面向社会提供服务的需要备案,那么不提供服务且有意阻止社会人员访问服务器的应用场景,如个人计算服务器、私人云盘(如群辉的家庭云服务),就不需要备案了。

用python实现DDNS

from aliyunsdkcore.client import AcsClient from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import DescribeDomainRecordsRequest from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest import json import subprocess import re # 获取当前域名对应的ip地址 def get_internet_ip(): child = subprocess.Popen("nslookup nideyuming", shell=True, stdout=subprocess.PIPE) out = child.communicate() # 保存nslookup获取的所有信息 ipv6_pattern = '(([a-f0-9]{1,4}:){7}[a-f0-9]{1,4})' m = re.findall(ipv6_pattern, str(out)) address = m[0][0] # 拿到ipv6地址 return address # 获取本机临时IPv6地址 def get_local_ip(): child = subprocess.Popen("ipconfig", shell=True, stdout=subprocess.PIPE) out = child.communicate() # 保存ipconfig中的所有信息 ipv6_pattern = '(([a-f0-9]{1,4}:){7}[a-f0-9]{1,4})' m = re.findall(ipv6_pattern, str(out)) address = m[1][0] return address # 获取recordid def get_recordid(client): request = DescribeDomainRecordsRequest() request.set_accept_format('json') request.set_DomainName("discover304.top") #\TODO 传你的域名 response = client.do_action_with_exception(request) response = str(response, encoding='utf-8') result = json.loads(response) recordid = result["DomainRecords"]["Record"][0]["RecordId"] return recordid # 更新域名解析 def update_record(client, priority, ttl, record_type, value, rr, record_id): request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_Priority(priority) request.set_TTL(ttl) request.set_Value(value) request.set_Type(record_type) request.set_RR(rr) request.set_RecordId(record_id) response = client.do_action_with_exception(request) response = str(response, encoding='utf-8') return response # 函数调用 def run_result(client, priority, ttl, record_type, value, rr, record_id): if get_local_ip() == get_internet_ip(): print("解析地址未发生变化,不修改!") pass else: result = update_record(client, priority, ttl, record_type, value, rr, record_id) result = json.loads(result) print("已更新解析地址,新的RecordId是:%s" % result["RecordId"]) if __name__ == '__main__': # \TODO 这儿传你的accessKeyId、accessSecret和区域ID(region_id) client = AcsClient('xxxx', 'xxxx', 'cn-beijing') record_id = get_recordid(client) ip = get_local_ip() run_result(client, "5", "600", "AAAA", ip, "@", record_id)

参考

原文链接:https://blog.csdn.net/Discover304/article/details/121990901?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168476299816800225515071%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168476299816800225515071&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-13-121990901-null-null.blog_rank_default&utm_term=NAS%E3%80%81%E7%BE%A4%E6%99%96%E3%80%81%E9%98%BF%E9%87%8C%E4%BA%91%E3%80%81%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90%E3%80%81%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F%E3%80%81ipv6%E3%80%81ddns%E3%80%81%E8%BD%BB%E9%87%8F%E7%BA%A7%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E3%80%81%E9%93%81%E5%A8%81%E9%A9%AC%E3%80%81%E5%A8%81%E8%81%94%E9%80%9A%E3%80%81DSM%E3%80%81DSM6.0%E3%80%81%E7%BE%A4%E6%99%96nas%E3%80%81%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E3%80%81%E8%9C%97%E7%89%9B%E6%98%9F%E9%99%85%E3%80%81%E9%BB%91%E7%BE%A4%E6%99%96%E3%80%81docker%E3%80%81%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E3%80%81%E5%9F%9F%E5%90%8D%E6%B3%A8%E5%86%8C%E3%80%81%E5%AE%9D%E5%A1%94%E3%80%81%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E3%80%81nginx%E3%80%81frp%E3%80%81%E5%8A%A8%E6%80%81%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享