樹莓派 之 動(dòng)態(tài)DNS(DNSPod)

需注冊DNSPod的賬號,如果做Server的樹莓派IP總是變動(dòng)的話,可以用這個(gè)實(shí)時(shí)更新域名的IP
腳本來自github,版權(quán)歸屬原作者chuangbo,以下說明引自README
替換上你的Email,密碼,域名ID,記錄ID等參數(shù),就可以運(yùn)行了。 會(huì)在后臺一直運(yùn)行,每隔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="默認(rèn)",
)
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)

開機(jī)自運(yùn)行:

  • sudo nano /etc/rc.local

  • exit 0

前添加:

  • python /usr/bin/pypod.py

不過似乎自啟和Gitlab有沖突

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容