树莓派 之 动态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有冲突

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,497评论 19 139
  • 屏幕分辨率 修改SD卡中的config.txt。可以通过sudo nano /boot/config.txt配置 ...
    一录小跑阅读 5,518评论 0 1
  • 将你家里的电脑连上广域网 我的博客托管在 github 提供的静态文本服务中,之前买的主机也到期了,所以就选择了方...
    薛晓刚阅读 3,462评论 0 2
  • (一) 小A刚开始周末给我发微信,欲言又止。我就直接给她打电话问什么情况,刚开始还忍着,后来实在忍不住了,电话那边...
    雪暮须臾阅读 1,800评论 0 0
  • 今天,距离2013年参加高考,已经整整过了三年。 过去的三年里,一直在大学的无辜年生里忙忙碌碌,那些从容不迫的光阴...
    远方之远阅读 2,325评论 5 4