需注冊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有沖突