-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdnspod4ddns.py
More file actions
66 lines (60 loc) · 1.99 KB
/
dnspod4ddns.py
File metadata and controls
66 lines (60 loc) · 1.99 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#coding:utf-8
#author:zhangnq
#url:http://www.sijitao.net
#description:使用dnspod的api实现ddns的目的
import urllib2
import urllib
import json
import os
import datetime
username='xxx'
password='xxx'
type1='json'
domain_id='xxx'
record_id='xxx'
def MonitorRecordChange():
#获取本地ip
os.system('export PATH=$PATH;curl -o /tmp/ipinfo.txt http://ipinfo.io/ >/dev/null 2>&1')
f=file('/tmp/ipinfo.txt','rb')
localip=json.loads(f.read())['ip']
f.close()
#获取dnspod记录
url='https://dnsapi.cn/Record.Info'
values={
'login_email':username,
'login_password':password,
'format':type1,
'domain_id':domain_id,
'record_id':record_id,
}
data=urllib.urlencode(values)
req=urllib2.Request(url,data)
response = urllib2.urlopen(req)
recordip=json.loads(response.read())['record']['value']
#修改记录
if localip != recordip:
url='https://dnsapi.cn/Record.Modify'
values={
'login_email':username,
'login_password':password,
'format':type1,
'domain_id':domain_id,
'record_id':record_id,
'sub_domain':'sijitao.net',
'value':localip,
'record_type':'A',
'record_line':'默认',
}
data=urllib.urlencode(values)
req=urllib2.Request(url,data)
try:
response = urllib2.urlopen(req)
print "Domain Record Change Successfully!%s --> %s" % (recordip,localip)
except:
print "Domain Record Change Failed!%s --> %s" % (recordip,localip)
else:
print "%s Domain Record Is Not Need To Change." % datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if __name__ == '__main__':
MonitorRecordChange()