树莓派 之 动态DNS(DNSPod)

需注册DNSPod的账号,如果做Server的树莓派IP总是变动的话,可以用这个实时更新域名的IP
脚本来自github,版权归属原作者chuangbo,以下说明引自README
替换上你的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"

sudo nano /usr/bin/pypod.py

pypod.py

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

import httplib, urllib, urllib2
import socket
import time
import re

urllib2_urlopen = urllib2.urlopen
re_findall = re.findall

params = dict(
    login_email="ID", # replace with your email
    login_password="12345678", # replace with your password
    format="json",
    domain_id= , # replace with your domain_od, can get it by API Domain.List
    record_id= , # 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():
    try:
        ip = re_findall(r"\[.+\]", urllib2_urlopen("http://1111.ip138.com/ic.asp", timeout=10).read())[0][1:-1]
    except Exception, e:
        print e
        pass
    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)

开机自运行:

  • sudo nano /etc/rc.local

  • exit 0

前添加:

  • python /usr/bin/pypod.py

不过似乎自启和Gitlab有冲突

你可能感兴趣的:(树莓派 之 动态DNS(DNSPod))