Ubuntu 15.10 下动态解析DDNS

  1. sudo apt-get install curl
  2. curl -k https://dnsapi.cn/Domain.List -d “login_email=druculac@gmail.com&login_password=456789ZC”

找到其中id为61220104,这就是domain id,可能有多个,我这里找的是dormanthink.cc

  1. curl -k https://dnsapi.cn/Record.List -d “login_email=druculac@gmail.com&login_password=456789ZC&domain_id=945962”

记录想要更新的域名的Record id,我的如下:

 

www:317419045

我要更新的为www.dormanthink.com

  1. 然后在ultraedit里面新建一个文档ddns.py,内容如下:

#!/usr/bin/env python

#-*- coding:utf-8 -*-

 

import httplib, urllib

import socket

import time

 

params = dict(

    login_email=”druculac@gmail.com”, # replace with your email

    login_password=”456789ZC”, # replace with your password

    format=”json”,

    domain_id=945962, # replace with your domain_od, can get it by API Domain.List

    record_id=62577664, # 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),30)

    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(300)

这里一定要注意,不要用远程登录的vi来写入这段代码,不然怎么弄都不是utf-8格式,那么执行的时候总会报错record line invalid。最好用ultraedit来保存。另外每行不要空两格。

  1. 保存的时候选择编码为utf-8,文件名为ddns.py
  2. 通过ftp上传到服务器的richard home。
  3. 通过python ddns.py即可执行。
  4. 加入到开机运行,sudo nano /etc/rc.local
  5. 在exit 0之前加入如下代码:
  6. /usr/bin/python /home/richard/ddns.py &

记得一定要加最后的“&”


评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注