本帖最后由 BG6RSH 于 2019-7-6 22:38 编辑
实现条件:宽带要得是公网IP,这个很重要。
1、花9元钱在阿里云上云注册一个“.top”的顶级域名,再利用下面的代码定时更新你家路由器的IP,这样就实现了DDNS的效果了。
2、将Python程序文件aliyundns.py复制到共享目录中,如复制到\\192.168.3.8\public\下。
3、获取阿里云的AccessKey和AccessKey,方法见https://helpcdn.aliyun.com/knowledge_detail/38738.html。
4、在群晖上安装阿里云Python SDK运行环境,请看安装方法http://bbs.naslink.top:5081/viewtopic.php?f=1&t=9,这里就不详说了。
5、在群晖计划任务中添加一条命令,10分钟执行一次:- #!/bin/sh
- python /volume1/public/aliyundns.py www.naslink.top
复制代码
如果想生成记录文档备查,就在每条命令加如下指令:
- #!/bin/sh
- python /volume1/public/aliyundns.py www.naslink.top >> /volume1/public/ddns.log
- python /volume1/public/aliyundns.py nas.atoo.top >> /volume1/public/ddns.log
复制代码 6、Python程序文件aliyundns.py内容如下:
- # 阿里支DNS自动更新IP地址程序(Python)
- # 文件名 : aliyundns.py
- # 调用方法: python aliyundns.py www.naslink.top
- #!/usr/bin/env python
- #coding=utf-8
- import sys
- import json
- import urllib
- import datetime
- from aliyunsdkcore.client import AcsClient
- from aliyunsdkcore.acs_exception.exceptions import ClientException
- from aliyunsdkcore.acs_exception.exceptions import ServerException
- from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest
- from aliyunsdkalidns.request.v20150109.DescribeSubDomainRecordsRequest import DescribeSubDomainRecordsRequest
- if len(sys.argv)<=1:
- print 'Usage: python aliyundna.py www.naslink.top'
- sys.exit(0)
-
- # 将下面两个引号中的字符串内容换成你自己的,这个非常重要。
- accessKeyId = 'LTAIjOE53hvdwBfI'
- accessSecret = 'JlvkWaCPek3SccYaFM9z14JGQbd8Zp'
- DomainName = sys.argv[1]
- client = AcsClient(accessKeyId, accessSecret, 'cn-hangzhou')
- request = DescribeSubDomainRecordsRequest()
- request.set_accept_format('json')
- request.set_SubDomain(DomainName)
- response = client.do_action_with_exception(request)
- response = json.loads(response.decode())
- RecordId = response['DomainRecords']['Record'][0]['RecordId']
- RR = response['DomainRecords']['Record'][0]['RR']
- old_ip = response['DomainRecords']['Record'][0]['Value']
- new_ip = urllib.urlopen("http://ip.42.pl/raw").read()
- if old_ip != new_ip:
- request = UpdateDomainRecordRequest()
- request.set_accept_format('json')
- request.set_Value(new_ip)
- request.set_Type("A")
- request.set_RR(RR)
- request.set_RecordId(RecordId)
- response = client.do_action_with_exception(request)
- print datetime.datetime.now(), ' ', DomainName,': ', old_ip, ' => ', new_ip
- else :
- print datetime.datetime.now(), ' ', DomainName, ': IP has not changed.', new_ip
复制代码
|
|