Skip to content

Instantly share code, notes, and snippets.

@alal
Forked from chuangbo/README.md
Last active May 22, 2017 13:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alal/c0c79b740303d12350fc to your computer and use it in GitHub Desktop.
Save alal/c0c79b740303d12350fc to your computer and use it in GitHub Desktop.
Python dynamic DNSPod DNS Script

先在在dnspod建一条目标记录, 如 ddns. 替换上你的参数,就可以运行了。 会在后台一直运行,每隔30秒检查一遍IP,如果修改了就更新IP。

获得domain_id可以用curl (找到你目标域名) curl -k https://dnsapi.cn/Domain.List -d "login_email=xxx&login_password=xxx"

获得record_id类似 (找到你目标子域名) curl -k https://dnsapi.cn/Record.List -d "login_email=xxx&login_password=xxx&domain_id=xxx"

#!/usr/bin/env python2
# -*- coding:utf-8 -*-
import httplib
import urllib
import socket
import time
# Use Token, check https://support.dnspod.cn/Kb/showarticle/tsid/227/
ID = "19999" # relace with yours, get it as link above show you.
Token = "8eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" # relace with yours, get it as above link show you.
params = dict(
login_token=("%s,%s" % (ID, Token)),
format="json",
domain_id=1999999, # replace with your domain_od, can get it by API Domain.List
record_id=199999999, # replace with your record_id, can get it by API Record.List
sub_domain="ddns", # 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), 20)
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 as e:
print e
pass
time.sleep(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment