Skip to content

Instantly share code, notes, and snippets.

@chuangbo
Last active June 19, 2023 04:48
Show Gist options
  • Save chuangbo/833369 to your computer and use it in GitHub Desktop.
Save chuangbo/833369 to your computer and use it in GitHub Desktop.
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)
Copy link

ghost commented Jul 26, 2015

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

@pandalid
Copy link

然而得到的ip不是真的ip

@alal
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
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
Copy link

dawncold commented Jun 4, 2017

@senhtry 这个接口无法修改ttl

@mliuchengyu
Copy link

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

@snullp
Copy link

snullp commented Jul 26, 2019

@rankun203
Copy link

rankun203 commented Aug 30, 2022

getip failed for me, this worked

def getip():
    res = requests.get("http://www.net.cn/static/customercare/yourip.asp")
    matches = re.search('(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})', res.text)
    ip = matches.group(0)
    return ip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment