脚本是Python写的,树莓派上可直接运行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #!/usr/bin/env python #-*- coding:utf-8 -*- import httplib, urllib import socket import time params = dict ( login_email = "email" , # replace with your email login_password = "password" , # replace with your password format = "json" , domain_id = 100 , # replace with your domain_od, can get it by API Domain.List record_id = 100 , # replace with your record_id, can get it by API Record.List sub_domain = "www" , # replace with your sub_domain record_line = "默认" , ) current_ip = None def ddns(ip): params.update( dict (value = ip)) headers = { "Content-type" : "application/x-www-form-urlencoded" , "Accept" : "text/json" } conn = httplib.HTTPSConnection( "dnsapi.cn" ) conn.request( "POST" , "/Record.Ddns" , urllib.urlencode(params), headers) response = conn.getresponse() print response.status, response.reason data = response.read() print data conn.close() return response.status = = 200 def getip(): sock = socket.create_connection(( 'ns1.dnspod.net' , 6666 )) ip = sock.recv( 16 ) sock.close() return ip if __name__ = = '__main__' : while True : try : ip = getip() print ip if current_ip ! = ip: if ddns(ip): current_ip = ip except Exception, e: print e pass time.sleep( 30 ) |
替换上你的Email,密码,域名ID,记录ID等参数,就可以运行了。 会在后台一直运行,每隔30秒检查一遍IP,如果修改了就更新IP。
获得domain_id可以用
1 | curl -k https: //dnsapi .cn /Domain .List -d "login_email=xxx&login_password=xxx" |
获得record_id类似
1 | curl -k https: //dnsapi .cn /Record .List -d "login_email=xxx&login_password=xxx&domain_id=xxx" |
加入开机自动运行的方法:
编辑/etc/rc.local文件,在“exit 0”那一行前面加上一行
1 | su root -c "python /home/pypod.py" |
更新:
新的 DNSPod API 有调整,参考:
https://www.dnspod.cn/docs/domains.html#domain-list
https://support.dnspod.cn/Kb/showarticle/tsid/227/