#!/usr/bin/env python # -*- coding:utf-8 -*- import os from aliyunsdkcore import client from aliyunsdkcore.request import RpcRequest product="Domain" version="2016-05-11" accesskey="XXXXXXXXXXXXXXXXXXXX" #请替换成自己的accesskey accesspasswd="XXXXXXXXXXXXXXXXXXXXXXXXXXXX" #请替换成自己的accesspasswd def getip(): return os.popen("curl http://members.3322.org/dyndns/getip -s").read().replace('\n','').replace('\r\n','') def getDNSrecords(): global product,version,accesskey,accesspasswd clt = client.AcsClient( accesskey, accesspasswd, 'cn-hangzhou') request=RpcRequest('Alidns', '2015-01-09', 'DescribeDomainRecords') request.add_query_param("DomainName","XXXXXXXXXXXXXXX") #请替换成自己的域名 request.set_accept_format('json') response=clt.do_action(request) return eval( response.replace('false','0') ) def setDNSrecord(record,ip): global product,version,accesskey,accesspassw clt = client.AcsClient( accesskey, accesspasswd, 'cn-hangzhou') request=RpcRequest('Alidns', '2015-01-09', 'UpdateDomainRecord') request.add_query_param("RecordId",record['RecordId']) request.add_query_param("RR",record['RR']) request.add_query_param("Type",record['Type']) request.add_query_param("Value",ip) request.set_accept_format('json') response=clt.do_action(request) print response if __name__=="__main__": ip=getip() print ip recordlist=getDNSrecords() for record in recordlist["DomainRecords"]["Record"]: setDNSrecord(record,ip)
最近在阿里注册了个域名,采用orangepi作自己的服务器,以前ddns用的nat123,但是感觉要注册很多东西,所以操作不是很方便,所以研究了下阿里的API,用python实现起来看着也比较简单,所以就搞了个脚本,实现了去DNS中更改域名的解析配置,这样当路由器断电后可以从服务器主动发起更高配置操作,把新的IP添加到域名解析中,以此实现动态域名解析。
网上也有几个版本的调用阿里API进行ddns的,不过代码有点长,所以自己写了一下,总共也就30几行吧,哈哈哈