pyton脚本
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import httplib, urllib
import socket
import time
import json
ddns_params = dict(
# 动态域名解析借口传参,具体看官网的接口文档
login_token="35140,98d2b22f9b07af3f3473d71a5fa8e34b", # replace with your login_token
format="json",
domain_id=56731616, # replace with your domain_od, can get it by API Domain.List
record_id=288532101, # replace with your record_id, can get it by API Record.List
sub_domain="test", # replace with your sub_domain
record_line_id=0,
)
record_params = dict(
# 请求域名ID为56731616的域名的记录传参,具体看官网的接口文档
login_token="35140,98d2b22f9b07af3f3473d71a5fa8e34b", # replace with your login_token
format="json",
domain_id=56731616, # replace with your domain_od, can get it by API Domain.List
)
current_ip = None
def ddns(ip):
# 调用动态域名解析接口
ddns_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(ddns_params), headers)
response = conn.getresponse()
print 'ddns',response.status, response.reason
data = response.read()
print 'ddns',data
conn.close()
return response.status == 200
def getip():
# 获取本地外网ID
sock = socket.create_connection(('ns1.dnspod.net', 6666))
ip = sock.recv(16)
sock.close()
return ip
def getRecordip():
# 获取ID为288532101的记录的ip
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"}
conn = httplib.HTTPSConnection("dnsapi.cn")
conn.request("POST", "/Record.List", urllib.urlencode(record_params), headers)
response = conn.getresponse()
print 'record',response.status, response.reason
data = response.read()
print 'record',data
conn.close()
data = json.loads(data)
record_list = data.get('records')
for r in record_list:
if r.get('id') == '288532101':
return r.get('value')
return None
if __name__ == '__main__':
current_ip = getRecordip()
print current_ip
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)