Sunday, September 27, 2015

DNSPod动态IP解析更新程序

获得domain_id:

curl -k http://bit.ly/1t1JbjL -d "login_email=xxx&login_password=xxx"

获得record_id:

curl -k http://bit.ly/1n9m5rO -d "login_email=xxx&login_password=xxx&domain_id=xxx"

在下面的Python脚本中替换上你的Email,密码,域名ID,记录ID等参数,就可以了。

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

我是将pypod.py文件放在/usr/local/bin/目录下的,注意修改自己的路径。

加入开机自动运行的方法:
编辑/etc/rc.local文件,在“exit 0”那一行前面加上一行:

su root -c "python /usr/local/bin/pypod.py >/tmp/dnspod.log 2>&1"

更好的方法就是添加cron任务,每两分钟检查一次:

sudo crontab -e
*/2 * * * * python /usr/local/bin/pypod.py >/tmp/dnspod.log 2>&1

转自:http://bit.ly/1Fr8qIQ

via Chu's Space http://bit.ly/1Wq6KDI

Powered by Blogger Skins. Theme: TheBuckmaker | Free Wordpress Templates. Presents HD TV Futurama Streaming. Featured on Wedding Dresses.