Raspberry使用Dnspod 动态域名更新客户端(Python)

树莓派有了PPPoE拨号后,我们可以使用DDNS动态更新域名,这样子,即使断线重连我们也能管理我们的Raspberry.

# Shell 版

人家已经做好了。Shell版本可以用于系统空间小,没装Python 的Openwrt 的环境,当然其他Linux/uinx下也是阔以的。

linux/FreeBSD下的dnspod动态域名客户端


# Python 版

参考官方的API,以及Demo写了出来。 支持多子域名,由于时间少,只完成了我需要的功能,其他复杂功能,你可以自己添加。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#blog:linsir.org
 
import httplib, urllib
import re, urllib2, json
import socket, sys


domain = "domain.com"
sub_domain = ['@',]
email = "admin@dnspod.cn"# replace with your email
password = "passwd"# replace with your password

class dnspod(object):
    """docstring for dnspod"""
    def __init__(self):
        self.params = {
            "login_email" : email,
            "login_password" : password,
            "format" : "json",
            "record_line" : "默认",
        }
    
    def get_public_ip(self):
        data = urllib2.urlopen("http://20140507.ip138.com/ic.asp").read()
        ip = re.search('\\d+\\.\\d+\\.\\d+\\.\\d+',data).group(0)
        return ip

    def get_domain_ip(self):
        result=socket.getaddrinfo(domain,'http')[0][4][0]
        return result
    
    def post(self,method):
        headers = {"UserAgent":"Ddns Client/0.1.0(vi5i0n@hotmail.com)",
                    "Content-type": "application/x-www-form-urlencoded",
                    "Accept": "application/json, text/javascript, */*; q=0.01"
                    }
        conn = httplib.HTTPSConnection("dnsapi.cn")
        conn.request("POST", "/"+method, urllib.urlencode(self.params), headers)
        response = conn.getresponse()

        data = response.read()
        conn.close()
        return data

    def get_domain_id(self):
        self.params.update(dict(domain=domain))
        data = self.post("Domain.Info")
        data = json.loads(data)
        domain_id = data["domain"]["id"]
        return domain_id

    def get_record_id(self):
        record_ids = {}
        domain_id = self.get_domain_id()
        self.params.update(dict(domain_id=domain_id))
        data = self.post("Record.List")
        data = json.loads(data)
        # domain_id = re.search('\\d{5,9}',data).group(0)
        for record in data["records"]:
            if record["type"] == 'A':
                if record["name"] in sub_domain:
                    name = record["name"]
                    record_ids[name] = record["id"]
                
        # print record_ids
        return record_ids

    def update_record(self, ip):
        ids = self.get_record_id()
        for sub in ids:
            self.params.update(dict(sub_domain=sub))
            self.params.update(dict(record_id=ids[sub]))
            self.params.update(dict(value=ip))
            data = self.post("Record.Ddns")
            data = json.loads(data)
        return data['status']['code']
               
 
if __name__ == '__main__':
    dns = dnspod()
    try:
        domain_ip = dns.get_domain_ip()
        public_ip = dns.get_public_ip()
        if domain_ip != public_ip:
            if dns.update_record(public_ip):
                print "Okay,updated with: %s!" %public_ip
        print "The IP:%s is right now." %public_ip
    except:
        print "Domain or Network Conncetion Error!Please check it out!"

# 加入crontab 任务定时执行。

    crontab -e
    */5 * * * *  python /root/dnspod.py >/dev/null

Okay,enjoy it .


# 参考

  1. linux/FreeBSD下的dnspod动态域名客户端
  2. DNSPod Documentation 1.0

--EOF--


>看不到评论?GFW!!!