盒子
盒子

dnspod动态域名解析

dnspod动态域名解析,write up.

添加DNSPOD记录

api文档https
动态解析demo

获得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"

开机的时候自动运行这个脚本 ,在/etc/rc.local中加入一行

1
2
# run pypod.py ddns
/usr/bin/python /home/pi/pypod.py

DNSPOD动态域名解析,ip获取有误修复

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
#-*- coding:utf-8 -*-

import httplib, urllib
import socket
import time
import json

params = dict(
login_email="hisen@xx.com", # replace with your email
login_password="pwd", # replace with your password
format="json",
domain_id=xxxxxx, # replace with your domain_od, can get it by API Domain.List
record_id=xxxxxx, # replace with your record_id, can get it by API Record.List
sub_domain="pi", # 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():
url='http://ip.taobao.com/service/getIpInfo.php?ip=myip'
page=urllib.urlopen(url)
data=page.read()
jsondata=json.loads(data)
ip = jsondata['data']['ip']
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(30)