加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
aliyun_ddns_p2.py 4.54 KB
一键复制 编辑 原始数据 按行查看 历史
unicoder5191 提交于 2020-03-21 23:56 . Input your access key id.
#!/usr/bin/python
#-*- encoding:UTF-8 -*-
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkalidns.request.v20150109.DescribeSubDomainRecordsRequest import DescribeSubDomainRecordsRequest
from aliyunsdkalidns.request.v20150109.AddDomainRecordRequest import AddDomainRecordRequest
from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest
import os
import time
import logging
from urllib2 import urlopen
import codecs
import json
import time
#coding=utf-8
def get_internet_ip_debug():
print("ip: 114.254.15.113")
return "114.254.15.113"
def get_internet_ip():
response = urlopen('http://www.3322.org/dyndns/getip')
html = response.read()
ip = str(html).replace("\n", "")
print('get_new_ip:'+ip)
return ip
def write_to_file(filePath,content):
fopen = codecs.open(filePath,'w',encoding='utf8')
fopen.write(content)
fopen.close()
def describe_SubDomain_Records(client,domain_type,subDomain):
request = DescribeSubDomainRecordsRequest()
request.set_accept_format('json')
request.set_SubDomain(subDomain)#"www.unicoder.xyz"
request.set_Type(domain_type)#"A"
#request.set_UserClientIp("114.254.18.113")
#request.set_DomainName("unicoder.xyz")
response = client.do_action_with_exception(request)
print(response)
#print(str(response, encoding='utf-8'))
# python3: response = str(response, encoding='utf-8')
response = str(response)
result = json.loads(response)
return result
def add_record(client,TTL,domain_type,value,RR,domain_name):
request = AddDomainRecordRequest()
request.set_accept_format('json')
request.set_TTL(TTL)
request.set_Type(domain_type)#"A"
request.set_Value(value)#"114.254.18.113"
request.set_RR(RR)#"www"
request.set_DomainName(domain_name) #"unicoder.xyz"
response = client.do_action_with_exception(request)
print(response) # python2
#print(str(response, encoding='utf-8'))
# python3: response = str(response, encoding='utf-8')
response = str(response)
result = json.loads(response)
return result
def update_record(client,TTL,domain_type,value,RR,record_id):
request = UpdateDomainRecordRequest()
request.set_accept_format('json')
request.set_TTL(TTL)
request.set_Type(domain_type)
request.set_Value(value)
request.set_RR(RR)
request.set_RecordId(record_id)
response = client.do_action_with_exception(request)
response = str(response, encoding='utf-8')
# python2: print(response)
#print(str(response, encoding='utf-8'))
print(response)
return response
logging.basicConfig(filename="/home/cubie/work/ddns/ali_ddns.log",filemode="a",format="%(asctime)s-%(name)s-%(levelname)s-%(message)s",level=logging.INFO)
client = AcsClient('YourAccessKeyID', 'YourAccessKeySecret', 'cn-hangzhou')
while True:
#ip = get_internet_ip_debug()
ip = get_internet_ip()
with open("/home/cubie/work/ddns/ip",'r') as f:
old_ip = f.read()
if ip == old_ip: #查询来的IP和当前IP一样,IP没有变化,不进行操作,记录下来。
print("noupdate"+"\nnew_ip:"+ip+"\nold_ip:"+old_ip)
logging.info(u"没有更新,\n新ip:"+ip+u"\n旧ip:"+old_ip)
else: #如果查询来的ip和当前ip不一致,则用查询来的ip更新域名解析记录。
write_to_file("/home/cubie/work/ddns/ip",ip)
des_result = describe_SubDomain_Records(client,'A',"www.unicoder.xyz")
if des_result["TotalCount"] == 0 : #子域名查询结果,如果不存在www.unicoder.xyz子域名,新建一个。
add_result = add_record(client,"600","A",ip,"www","unicoder.xyz")
record_id = add_result["RecordId"]
print("域名解析新增成功!")
logging.info(u"域名解析新增成功!")
elif des_result["TotalCount"] == 1 : #子域名查询结果,存在www.unicoder.xyz解析记录,则用新的ip更新一次。
try:
record_id = des_result["DomainRecords"]["Record"][0]["RecordId"]
update_record(client,"600","A",ip,"www",record_id)
print("域名解析更新成功!")
logging.info(u"域名解析更新成功!")
except Exception as err:
logging.warning("update_record has error!,look below."+str(ip)+" "+str(record_id))
logging.warning(str(Exception)+" "+str(err))
else:
record_id = 0
print("存在两个子域名解析记录值,请核查删除后再操作!")
logging.info(u"存在两个子域名解析记录值,请核查删除后再操作!")
write_to_file("/home/cubie/work/ddns/RecordId",record_id)
time.sleep(1200) #每20分钟查看一次ip变化情况,若变化则更新dns。
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化