#!/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添加到域名解析中,以此實現動態域名解析。python
網上也有幾個版本的調用阿里API進行ddns的,不過代碼有點長,因此本身寫了一下,總共也就30幾行吧,哈哈哈json