NAS群晖

NAS 动态更换阿里云 DDNS 解析

2019-11-19  本文已影响0人  游城十代2dai

0x00 代码

废话不多说, 上代码, 度娘也有很多的

#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import DescribeDomainRecordsRequest
from aliyunsdkcore.request import CommonRequest
import json
import os
import time
import logging
##################################
#阿里云提供的认证服务
accessKeyId='#你的 key'
#阿里云提供的认证服务   
accessSecret='#你的 secret'
client = AcsClient(accessKeyId, accessSecret, 'cn-beijing')
domain = '你的域名'


def updatednsip(wanip,recordid):
    global client
    print(type(wanip))
    print(type(recordid))
#    print('wanip:'+ wanip + 'recordid:' + str(recordid))
    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('alidns.aliyuncs.com')
    request.set_method('POST')
    request.set_protocol_type('https')  # https | http
    request.set_version('2015-01-09')
    request.set_action_name('UpdateDomainRecord')

    request.add_query_param('RecordId', recordid)
    request.add_query_param('RR', '@')
    request.add_query_param('Type', 'A')
    request.add_query_param('Value', wanip)

    response = client.do_action_with_exception(request)
    print(str(response))

def getcurdnsip():
    global client
    request = DescribeDomainRecordsRequest()
    request.set_DomainName(domain)
    request.set_action_name('DescribeDomainRecords')
    request.set_accept_format('json')
    response = client.do_action_with_exception(request)
    dic = json.loads(str(response))
    curdnsip = dic['DomainRecords']['Record'][0]['Value']
    recordid = dic['DomainRecords']['Record'][0]['RecordId']
    return curdnsip,recordid

def getwanip():

    res = os.popen('curl -s whatismyip.akamai.com')
    wanip = res.read()
    return wanip

def creatlogger():
    # create logger
    logger = logging.getLogger('ddnslog')
    logger.setLevel(logging.INFO)
    fh = logging.FileHandler('aliddns.log')
    fh.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    logger.addHandler(fh)
    return logger

if __name__ == '__main__':
    #创建日志
    logger = creatlogger()
    #获取当前路由器IP地址
    wanip = getwanip()
    #获取阿里云当前dns解析ip地址
    curdnsip,recordid = getcurdnsip()
    #将域名解析到路由器WAN地址
    if curdnsip != wanip:
        updatednsip(wanip,recordid)
        logger.info("wanip from '" + curdnsip + "'  change to '" + wanip + "'  runtime: " + time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))

0x01 Python 环境

阿里出品可以用 Python 2 也可以用 Python 3, 自行选择, 安装 pip 工具, 不同平台自行度娘

pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-mts
pip install aliyun-python-sdk-alidns
pip install aliyun-python-sdk-core-v3
pip install aliyun-python-sdk-mts
pip install aliyun-python-sdk-alidns

0x02 我的过程

  1. ssh 进入 NAS
# ~ [14:02:12]
$ ssh zwyxxd@121.204.5.22
zwyxxd@121.204.5.22's password:
zwyxxd@zwyxxd:~$ ls
domain_setting  Drive  scripts
zwyxxd@zwyxxd:~$
  1. 我的脚本在 scripts

  2. 切换 root 权限输入 sudo -i 在输入密码回车

  3. 执行 0x01 Python 环境 的命令

    aliyun-python-sdk-core
    aliyun-python-sdk-mts
    aliyun-python-sdk-alidns
  4. 环境配置完成后直接执行 python /path/aliyunddns.py

  5. 如果有报错是请求相关的, 请查看阿里云错误代码

  6. 以上均成功后就可以添加入 NAS 的计划任务中

# 此处换行执行两个命令
cd /volume1/homes/zwyxxd/scripts/aliyunddns 
python aliyunddns.py
上一篇下一篇

猜你喜欢

热点阅读