Created
January 10, 2014 08:20
-
-
Save winok/8348532 to your computer and use it in GitHub Desktop.
DDNS use dnspod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding:utf-8 -*- | |
import urllib2 | |
import urllib | |
import json | |
import time | |
import socket | |
public_dic={} | |
public_dic["login_email"]="" #replace your email | |
public_dic["login_password"]="" #replace your password | |
public_dic["format"]="json" | |
headers={} | |
headers["User-Agent"]="lixinDDNS/1(lixin@lixin.me)" | |
sleepTime=3000 | |
def WriteLog(msg): | |
f=open('./ddns.log','a') | |
f.write(time.strftime("%Y/%m/%d %H:%M:%S",time.localtime())+" "+msg+"\n") | |
f.close() | |
def getDomainID(domain): | |
url="https://dnsapi.cn/Domain.Info" | |
params=public_dic.copy() | |
params["domain"]=domain | |
req=urllib2.Request(url,headers=headers,data=urllib.urlencode(params)) | |
resp=urllib2.urlopen(req) | |
formatJson=json.load(resp) | |
if formatJson["status"]["code"]!="1": | |
WriteLog("getDomainID has Error: ("+formatJson["status"]["code"]+")"+formatJson["status"]["message"]) | |
return 0 | |
else: | |
return formatJson["domain"]["id"] | |
pass | |
def getRecordID(domain_id,record): | |
url="https://dnsapi.cn/Record.List" | |
params=public_dic.copy() | |
params["domain_id"]=domain_id | |
params["sub_domain"]=record | |
req=urllib2.Request(url,headers=headers,data=urllib.urlencode(params)) | |
resp=urllib2.urlopen(req) | |
myJson=json.load(resp) | |
if myJson["status"]["code"]!="1": | |
WriteLog("getRecordID has Error: ("+formatJson["status"]["code"]+")"+formatJson["status"]["message"]) | |
return 0 | |
else: | |
return myJson["records"][0]['id'] | |
pass | |
def getMyIp(): | |
url="ns1.dnspod.net" | |
port=6666 | |
mySocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM) | |
mySocket.connect((url,port)) | |
recv=mySocket.recv(16) | |
mySocket.close() | |
return recv | |
pass | |
def setDDNS(domainID,recordID,record): | |
url="https://dnsapi.cn/Record.Ddns" | |
params=public_dic.copy() | |
params["domain_id"]=domainID | |
params["sub_domain"]=record | |
params["record_id"]=recordID | |
params["record_line"]="默认" | |
req=urllib2.Request(url,headers=headers,data=urllib.urlencode(params)) | |
resp=urllib2.urlopen(req) | |
myJson=json.load(resp) | |
if myJson["status"]["code"]!="1": | |
WriteLog("setDDNS has Error: ("+myJson["status"]["code"]+")"+myJson["status"]["message"]) | |
pass | |
def getCurrIP(d,r): | |
return socket.gethostbyname(r+"."+d) | |
def handle(d,r): | |
domain=d | |
record=r | |
newIP=getMyIp() | |
oldIP=getCurrIP(domain,record) | |
if oldIP !=newIP: | |
domainID=getDomainID(domain) | |
if domainID!=0: | |
recordID=getRecordID(domainID,record) | |
if recordID !=0: | |
setDDNS(domainID,recordID,record) | |
WriteLog(record + "." + domain + " new ip="+newIP) | |
pass | |
pass | |
pass | |
if __name__ == '__main__': | |
handle('baidu.com','www') | |
handle('baidu.com','wiki') | |
### | |
### install crontab .... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment