一.通过AI助手实现一个ip-domain.py的脚本
起一个Python脚本,ip-domain.py;注意已安装Python3.+的运行环境;将下面阿里云相关配置添加,注意这里引用了两个包,requests和alibabacloud_alidns20150109;执行前需要安装一下,然后python ip-domain.py就可以尝试一下效果喽。
这里使用的是通义灵码辅助,虽然给出的提示无法直接执行,但是框架算是有了,然后结合阿里云API的Python版进行调整,最终结果如下:
这里分三个步骤:
①获取本地IP
②获取域名对应阿里云解析
③判断当前解析IP与本地域名IP不一致时,进行更新
# python ip-domain.pyimport json
# pip install requests
import requests
from datetime import datetime
# pip install alibabacloud_alidns20150109==3.2.0
from alibabacloud_alidns20150109.client import Client as Alidns20150109Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_alidns20150109 import models as alidns_20150109_models
from alibabacloud_tea_util import models as util_models# 阿里云相关配置
ALIYUN_ACCESS_KEY = "" # 阿里云查看accessKey
ALIYUN_SECRET_KEY = "" # 阿里云查看secretKey
ALIYUN_REGION_ID = "" # 替换为您的实际地域ID
ALIYUN_DOMAIN_NAME = "" # 待解析的域名
ALIYUN_SUBDOMAIN = "" # 带解析的子域名
ALIYUN_RECORD_TYPE = "" # 解析类型为A记录
ALIYUN_RECORD_LINE = "" # 默认线路或其他指定线路,默认default
ALIYUN_RECORD_ID = "" #解析记录的ID,可以通过域名列表F12查看def get_local_ip():try:response = requests.get("https://api.ipify.org")return response.text.strip()except requests.exceptions.RequestException as e:print(f"Error getting local IP: {e}")return Nonedef get_current_dns_record(client, recordId):describe_domain_record_info_request = alidns_20150109_models.DescribeDomainRecordInfoRequest(record_id=recordId)runtime = util_models.RuntimeOptions()try:# 复制代码运行请自行打印 API 的返回值response = client.describe_domain_record_info_with_options(describe_domain_record_info_request, runtime)print(f"GET DNS body: {response.body}")record_ip = response.body.valuereturn record_ipexcept Exception as error:# 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。# 错误 messageprint(f"GET DNS error: {error}")return Nonedef update_dns_record(client, recordId, ipAddress, subDomain, recordType):request = alidns_20150109_models.UpdateDomainRecordRequest(record_id=recordId,value=ipAddress,rr=subDomain,type=recordType)runtime = util_models.RuntimeOptions()try:# 复制代码运行请自行打印 API 的返回值response = client.update_domain_record_with_options(request, runtime)print(f"UPDATE DNS response: {response}")return responseexcept Exception as error:# 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。# 错误 messageprint(f"UPDATE DNS error: {error}")return Nonedef main():current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')# 初始化AcsClient实例config = open_api_models.Config( access_key_id=ALIYUN_ACCESS_KEY, access_key_secret=ALIYUN_SECRET_KEY )# Endpoint 请参考 https://api.aliyun.com/product/Alidnsconfig.endpoint = f'alidns.cn-hangzhou.aliyuncs.com'client = Alidns20150109Client(config)local_ip = get_local_ip()print(f"{current_time} Local IP: {local_ip}")dns_record_ip = get_current_dns_record(client, ALIYUN_RECORD_ID)print(f"{current_time} DNS Record IP: {dns_record_ip}")if local_ip is not None and local_ip != dns_record_ip:update_dns_record(client, ALIYUN_RECORD_ID, local_ip, ALIYUN_SUBDOMAIN, ALIYUN_RECORD_TYPE)print(f"{current_time} DNS record updated successfully.")else:print(f"{current_time} IP addresses match. No update required.")if __name__ == "__main__":main()
注意关于ALIYUN_RECORD_ID获取
二.找一个任务管理器进行调用,这里使用了nas上任务计划
控制面板,添加任务计划
用户这里选中root,否则需要授权,指定任务名称
制定一个每二十分钟执行一次的定时任务
将准备好的脚步执行语句填写上:
python “/volume1/docker/localip/ip-domain.py” >> “/volume1/docker/localip/log.txt” 2>&1
点击确定,就可以等待执行喽;查看一下执行日志,然后更新啦;
如果此时你家支持外网IP访问的话,就可以使用域名访问自己nas了。