- sudo apt-get install curl
- curl -k https://dnsapi.cn/Domain.List -d “login_email=druculac@gmail.com&login_password=456789ZC”
找到其中id为61220104,这就是domain id,可能有多个,我这里找的是dormanthink.cc
- curl -k https://dnsapi.cn/Record.List -d “login_email=druculac@gmail.com&login_password=456789ZC&domain_id=945962”
记录想要更新的域名的Record id,我的如下:
www:317419045
我要更新的为www.dormanthink.com
- 然后在ultraedit里面新建一个文档ddns.py,内容如下:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import httplib, urllib
import socket
import time
params = dict(
login_email=”druculac@gmail.com”, # replace with your email
login_password=”456789ZC”, # replace with your password
format=”json”,
domain_id=945962, # replace with your domain_od, can get it by API Domain.List
record_id=62577664, # 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():
sock = socket.create_connection((‘ns1.dnspod.net’, 6666),30)
ip = sock.recv(16)
sock.close()
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)
这里一定要注意,不要用远程登录的vi来写入这段代码,不然怎么弄都不是utf-8格式,那么执行的时候总会报错record line invalid。最好用ultraedit来保存。另外每行不要空两格。
- 保存的时候选择编码为utf-8,文件名为ddns.py
- 通过ftp上传到服务器的richard home。
- 通过python ddns.py即可执行。
- 加入到开机运行,sudo nano /etc/rc.local
- 在exit 0之前加入如下代码:
- /usr/bin/python /home/richard/ddns.py &
记得一定要加最后的“&”
发表回复