背景
公司电信宽带(PPoE)分配了一个公网IP,但它每过一段时间就变了,如果我要用它远程桌面,意味着每过一段时间就要改一次IP,记一个新的IP,非常复杂,该如何用一个固定的地址来替代这些变化的IP并且实现定时自动替换呢?虽然可以通过购买EIP来固定这个地址,但是成本稍微高了。替代的办法就是动态解析,用一个别名代替变换的IP。而别名就是域名,用一个固定的域名,解析到这些IP,即可实现DDNS。
原理
基本流程就是获取公网IP,然后修改某个域名的解析为获得的IP
流程图如下
实施
网络:
将服务器网络出口NAT到动态IP线路上(其他设置可自行科普,以下仅供参考)
此处已Cisco路由器为例:
ip nat inside source static tcp 192.168.0.217 80 interface Dialer1 11180 #Dialer1为PPOE拨号线路 access-list 100 permit ip host 192.168.0.217 any #ACL 100为PPOE拨号线路
密钥:
在确定服务器经由PPoE线路上网后,需要在DNSPod上申请API密钥,从而获取SecretId以及SecretKey
环境:
Ubuntu 20.04 + Python 3.8.10
安装依赖包:
pip install tencentcloud-sdk-python pip install tencentcloud-sdk-python-dnspod
**详细脚本如下: **
Ref: 腾讯云 API Explorer
import json import requests import logging from datetime import datetime 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 Domain = "*****.com" SubDomain = "p1234" SecretId = "SecretId" SecretKey = "SecretKey" filename = "./" + datetime.now().strftime("%Y%m%d") + "-" + SubDomain + "-DDNS-Log.log" logging.basicConfig(filename=filename, level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s') logging.info("脚本开始运行...") def get_external_ip(): try: ip = requests.get('https://checkip.amazonaws.com/').text.strip() return ip logging.info("成功获取当前公网地址...") except: return None logging.info("获取当前公网地址失败...") CurrentGlobalIP = get_external_ip() 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, "Subdomain": SubDomain } req.from_json_string(json.dumps(params)) resp = client.DescribeRecordList(req) RecordID = resp.RecordList[0].RecordId RecordValue = resp.RecordList[0].Value RecordLine = resp.RecordList[0].Line RecordType = resp.RecordList[0].Type logging.info("成功获取域名信息...") logging.info("域名信息: " + SubDomain+"."+Domain+" "+RecordValue) except TencentCloudSDKException as err: logging.info("获取域名信息失败..." + err) if CurrentGlobalIP == RecordValue: logging.info("域名信息未发生变动...") else: logging.info("域名信息发生变动...") try: req = models.ModifyRecordRequest() params = { "Domain": Domain, "RecordId": RecordID, "RecordLine": RecordLine, "RecordType": RecordType, "SubDomain": SubDomain, "Value": CurrentGlobalIP } req.from_json_string(json.dumps(params)) resp = client.ModifyRecord(req) logging.info("新域名信息: " + SubDomain + "." + Domain + " " + CurrentGlobalIP) except TencentCloudSDKException as err: logging.info("修改域名信息失败..." + err) logging.info("脚本结束运行...")
将脚本设置成定时运行
此处是将脚本设置为每小时的10分运行一次,也可按照自己需求进行修改。
日志输出:
到此,就完成了动态公网解析,更多可能,需要我们一起继续探索…
原文链接:https://blog.csdn.net/u012153104/article/details/129162140?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168476299816800188539640%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168476299816800188539640&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-28-129162140-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