Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

替换上你的Email,密码,域名ID,记录ID等参数,就可以运行了。 会在后台一直运行,每隔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 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
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()
if __name__ == '__main__':
while True:
try:
ip = getIpFrom_ip138()
print ip
if current_ip != ip:
if ddns(ip):
current_ip = ip
except Exception, e:
print e
pass
time.sleep(30)
@zxkletters

This comment has been minimized.

Copy link
Owner Author

zxkletters commented Sep 29, 2013

[ update]支持多个record的更新。


#!/usr/bin/env python
#-*- coding:utf-8 -*-

import httplib, urllib
import socket
import time
import re

params = dict(
    login_email="email", # replace with your email
    login_password="password", # replace with your password
    format="json",
    domain_id=123456, # replace with your domain_id, can get it by API Domain.List
    record_id="55555,666666", # replace with your record_id list,seperat with "," can get it by API Record.List
    #sub_domain="*", # replace with your sub_domain
    record_line="默认",
)

# record_id and sub_domain's mapping
record_domain_map = dict(
    s_55555="*",
    s_666666="@",
)

current_ip = None

def ddns(ip):
    recordList = params["record_id"].split(",")
    if not recordList:
        return

    flag = True
    for record in recordList:
        params.update(record_id=record)
        key = "s_%s" % record
        params.update(sub_domain=record_domain_map[key])
        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()
        flag &= response.status == 200

    return flag

def getip():
    sock = socket.create_connection(('ns1.dnspod.net', 6666))
    ip = sock.recv(16)
    sock.close()
    return 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()

if __name__ == '__main__':
    while True:
        try:
            ip = getIpFrom_ip138()
            print ip
            if current_ip != ip:
                if ddns(ip):
                    current_ip = ip
        except Exception, 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
You can’t perform that action at this time.