Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python dynamic DNSPod DNS Script

替换上你的 API Token,域名ID,记录ID等参数,就可以运行了。 会在后台一直运行,每隔30秒检查一遍IP,如果修改了就更新IP。

获取 API Token 的方式

获得 domain_id 可以用 curl

curl -k https://dnsapi.cn/Domain.List -d "login_token=TOKEN"`

获得 record_id 类似

curl -k https://dnsapi.cn/Record.List -d "login_token=TOKEN&domain_id=DOMAIN_ID"`
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import httplib
import urllib
import socket
import time
params = dict(
login_token="TOKEN", # replace with your API token, see https://support.dnspod.cn/Kb/showarticle/tsid/227/
format="json",
domain_id=DOMAIN_ID, # replace with your domain_id, can get it by API Domain.List
record_id=RECORD_ID, # replace with your record_id, can get it by API Record.List
sub_domain="www", # replace with your sub_domain
record_line="默认", # replace with your 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)
@haibinpark

This comment has been minimized.

Copy link

haibinpark commented Sep 6, 2012

than you for your share,but this script unstable,It has been terminated sometime
I run this script use above command
python pypod.py

@wschacker

This comment has been minimized.

Copy link

wschacker commented Sep 24, 2012

很好,正是我想要的

@wschacker

This comment has been minimized.

Copy link

wschacker commented Sep 24, 2012

是否可以修改为 纯 shell的,不使用 python? 因为 电信的 猫不支持 python 脚本,

@chuangbo

This comment has been minimized.

Copy link
Owner Author

chuangbo commented Nov 1, 2012

@wschacker 那你的服务器上有 curl 吗

@lo0kup

This comment has been minimized.

Copy link

lo0kup commented Dec 19, 2012

不错,脚本运行正常

@cloverstd

This comment has been minimized.

Copy link

cloverstd commented Dec 26, 2012

DNSPod 貌似现在更改了 API 接口了,会自动读取请求主机的 IP 并且更新记录
所以不需要自己读取 IP 了

@yuanwencong

This comment has been minimized.

Copy link

yuanwencong commented Dec 28, 2012

正需要这个

@MasonBai

This comment has been minimized.

Copy link

MasonBai commented Feb 3, 2013

脚本很好用,谢谢作者!

另:大家可以用“官方客户端Lite”在windows上测试一下,看其log记录的request的参数,就知道自己该填什么了

@complexray

This comment has been minimized.

Copy link

complexray commented Mar 12, 2013

执行之后提示如下

{"status":{"code":"26","message":"Record line invalid","created_at":"2013-03-12 09:49:15"}}

@mrmuxl

This comment has been minimized.

Copy link

mrmuxl commented Mar 14, 2013

@complexray ,估计是你的 record_line 参数不对,你web登陆一下你的域名线路是不是 “默认” ,如果你的web显示电信,那么你这里改成电信好了

@KavaDimon

This comment has been minimized.

Copy link

KavaDimon commented Mar 18, 2013

Thank you, that's great.It's so easy.But there is a question that the script may have stoped after days later, I must reboot my server or make the script work again.Why?

@jared-yan

This comment has been minimized.

Copy link

jared-yan commented Mar 30, 2013

to:KavaDimon
33: sock = socket.create_connection(('ns1.dnspod.net', 6666), 30) #try to set timeout 30s

@aguegu

This comment has been minimized.

Copy link

aguegu commented Apr 2, 2013

移动铁通环境下,没法得出正确的外网ip,都是异地的电信ip。

@ghost

This comment has been minimized.

Copy link

ghost commented Apr 5, 2013

给力啊!用上了!谢谢

@kevinzhangtao

This comment has been minimized.

Copy link

kevinzhangtao commented Apr 17, 2013

请教高手,如需更多二级域名的话,record_id和sub_domain 都会变化,需在代码上做什么改进变化呢?

record_id=100, # replace with your record_id, can get it by API Record.List
sub_domain="www", # replace with your sub_domain

record_id=200, # replace with your record_id, can get it by API Record.List
sub_domain="blog", # replace with your sub_domain

是否自行添加此两行就有效吗?

@iskl

This comment has been minimized.

Copy link

iskl commented May 18, 2013

萨克游!马克!

@daoser

This comment has been minimized.

Copy link

daoser commented May 28, 2013

def getip():
sock = socket.create_connection(('ns1.dnspod.net', 6666))
sock.settimeout(10)
ip = sock.recv(16)
sock.close()
return ip

sock.settimeout(10) 不能少了这句 不然 不超时 会死锁

@lakemove

This comment has been minimized.

Copy link

lakemove commented May 29, 2013

@wschacker

curl -X POST https://dnsapi.cn/Record.Ddns -d 'login_email=abc@example.com&login_password=abcdefg&format=json&domain_id=16835156&record_id=251579845&record_line=%E9%BB%98%E8%AE%A4'

shell上面这一行就可以了. dnspod加了对ddns的api支持 .它试图从request里读取要更新的ip

@zxkletters

This comment has been minimized.

Copy link

zxkletters commented Aug 17, 2013

getip() 获取的不是我真正的公网ip.
解决办法:通过ip138获取真实的ip.


def getIpFrom_ip138():
    conn = httplib.HTTPConnection('iframe.ip138.com')
    conn.request("GET", "/ic.asp")
    result = conn.getresponse().read()
    conn.close()

    m = re.search("([\d]+(\.)+[\d]+(\.)+[\d]+(\.)+[\d]+)+", result)
    if m:
        return m.group(0)
    else:
        return getip()
@daoser

This comment has been minimized.

Copy link

daoser commented Sep 24, 2013

@kevinzhangtao
两个简单方法实现多个域名
第一个:将你的blog 别名解析到 www上。这样你只要改www的域名就可以了
第二个:再跑一个脚本不就OK了

@long552900806

This comment has been minimized.

Copy link

long552900806 commented Apr 10, 2014

你好,我 对DNSPod API不是太了解,想跟您请教一下,能QQ聊吗?我QQ:552900806

@aa65535

This comment has been minimized.

Copy link

aa65535 commented Apr 24, 2014

getip函数有点问题,没有设置超时的话断线重连好像无法更新ip,建议加上超时:

def getip():
    sock = socket.create_connection(('ns1.dnspod.net', 6666), 10)
    ip = sock.recv(16)
    sock.close()
    return ip
@nelsonjin

This comment has been minimized.

Copy link

nelsonjin commented Feb 4, 2015

ns1.dnspod.net 6666端口无法连接 最近服务是不是出问题了?

@ghost

This comment has been minimized.

Copy link

ghost commented Jul 26, 2015

某坑爹宽带获取到的IP并不是真实IP

@pandalid

This comment has been minimized.

Copy link

pandalid commented Sep 14, 2015

然而得到的ip不是真的ip

@alal

This comment has been minimized.

Copy link

alal commented Dec 28, 2015

使用 token 代替用户名密码

#!/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)
@senhtry

This comment has been minimized.

Copy link

senhtry commented Dec 18, 2016

这个脚本设置的TTL=10,太短了,在python代码里加了行ttl=600还是不行,问题在哪?

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="默认",  #
    ttl=600,
)
@dawncold

This comment has been minimized.

Copy link

dawncold commented Jun 4, 2017

@senhtry 这个接口无法修改ttl

@liuchengyu

This comment has been minimized.

Copy link

liuchengyu commented May 24, 2019

image
账号密码都正确,为什么还会提示登陆失败呢?

@snullp

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.