您的位置:首页 > 编程语言 > Python开发

dnspod批量更新添加激活DNS解析【python脚本】

2014-12-31 17:16 483 查看
更新日志:
1.解决一个域名下拥有大量记录时的执行效率问题
2.优化代码逻辑
3.增加批量和cli两种方式
最新代码:
批量:https://github.com/coocla/dnspod-batch
Cli:https://github.com/coocla/dnspod-cli
做游戏的哥们都知道,每天大量的开服都意味着每天有大量的域名需要解析,这下可苦了我们公司的妹子,每天解析域名点的手疼,浪费生命啊。理所应当的一个批量解析的需求就轰轰烈烈的诞生了。参考文档:1.https://github.com/DNSPod/dnspod-python2.https://www.dnspod.cn/docs/index.html域名列表文本内容格式:域名 电信IP,联通IP
www.baidu.com 1.1.1.1,2.2.2.2 双线解析
www.sohu.com ,3.3.3.3 只做联通线路解析
music.baidu.com 4.4.4.4, 只做电信线路解析
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#who:czlinux@163.com
#when:2013-08-14
import os,sys
from dnspod.apicn import *
domain_list=[]
domain_dic={}
illegal_list=[]
login_email="XXXXX"
password="XXXXX"
domain_file="/root/dns"
def domain_index(domain_file):
all_domain_list=[]
for line in file(domain_file).readlines():
if line == '\n':
continue
if line[-1] == '\n':
line=line[:-1]
all_domain_list.append('.'.join(line.split()[0].split('.')[-2:]))
domain_list=list(set(all_domain_list))
api=DomainList(email=login_email,password=password)
all_msg=api().get("domains")
exists_list=[]
for i in range(0,len(all_msg)):
exists_list.append(all_msg[i].get("name"))
for dom in domain_list:
if dom not in exists_list:
api_create=DomainCreate(dom,email=login_email,password=password)
try:
domain_create=api_create()
all_msg=api().get("domains")
for n in range(0,len(all_msg)):
if dom == all_msg
.get("name"):
dom_id=all_msg
.get("id")
domain_dic[dom]=dom_id
except Exception,e:
illegal_list.append(dom)
#print "[Error:] %s" % e
print """\033[31m[Error]: Create domain [ %s ] Fail
.....You may not have permission to create it ! \033[0m""" % dom
else:
all_msg=api().get("domains")
for n in range(0,len(all_msg)):
if dom == all_msg
.get("name"):
dom_id=all_msg
.get("id")
domain_dic[dom]=dom_id
def add_record(domain,ip):
if len(ip.split(',')) == 2:
tel_ip=ip.split(',')[0]
unic_ip=ip.split(',')[1]
else:
print "\033[31m[Error]: IP format error...\033[0m"
#sys.exit()
sub_dom='.'.join(domain.split('.')[:-2])
dom_name=['.'.join(domain.split('.')[-2:])]
for k,v in domain_dic.iteritems():
if k in dom_name:
domain_id=domain_dic[k]
#print sub_dom   #debug
api_subdom=RecordList(domain_id,email=login_email,password=password)
try:
rec_list=api_subdom().get("records")
tmp_sub=[]
try:
rec_list=api_subdom().get("records")
tmp_sub=[]
for i in range(0,len(rec_list)):
tmp_sub.append(rec_list[i].get("name"))
if sub_dom in tmp_sub:
#print "[Error]: Sub_domain [ %s.%s ] exists..." % (sub_dom,'.'.join(domain.split('.')[-2:]))
update_record(rec_list,sub_dom,domain_id,'.'.join(domain.split('.')[-2:]),tel_ip,unic_ip)
else:
tel_record=RecordCreate(sub_dom,"A",u'默认'.encode("utf8"),tel_ip,600,domain_id=domain_id,email=login_email,password=password)
unic_record=RecordCreate(sub_dom,"A",u'联通'.encode("utf8"),unic_ip,600,domain_id=domain_id,email=login_email,password=password)
tel_record()
print "\033[32m[Notice]: Add record [ %s.%s ] -- [ %s ] successful!\033[0m" % (sub_dom,'.'.join(domain.split('.')[-2:]),tel_ip)
unic_record()
print "\033[32m[Notice]: Add record [ %s.%s ] -- [ %s ] successful!\033[0m" % (sub_dom,'.'.join(domain.split('.')[-2:]),unic_ip)
except:
print "\033[31m[Error]: Add sub_domain record some error...\033[0m"
#sys.exit()
def update_record(rec_list,sub_dom,domain_id,dom,tel_ip="",unic_ip=""):
for nu in range(0,len(rec_list)):
if sub_dom == rec_list[nu].get("name"):
record_id=rec_list[nu].get("id")
if rec_list[nu].get("line") == u"默认" and tel_ip:
try:
tel_update_record=RecordDdns(record_id,sub_dom,"默认",domain_id=domain_id,ttl=600,value=tel_ip,email=login_email,password=password)
tel_update_record()
print "\033[32m[Notice]: Update [ %s.%s ] --- [ %s ] Successful!\033[0m" % (sub_dom,dom,tel_ip)
except Exception,e:
print "[Error]: %s" % e
print "\033[31m.......... Update [ %s.%s ] --- [ %s ] Fail!\033[0m" % (sub_dom,dom,tel_ip)
elif rec_list[nu].get("line") == u"联通" and unic_ip:
try:
unic_update_record=RecordDdns(record_id,sub_dom,"联通",domain_id=domain_id,ttl=600,value=unic_ip,email=login_email,password=password)
unic_update_record()
print "\033[32m[Notice]: Update [ %s.%s ] --- [ %s ] Successful!\033[0m" % (sub_dom,dom,unic_ip)
except Exception,e:
print "[Error]: %s" % e
print "\033[31m.......... Update [ %s.%s ] --- [ %s ] Fail!\033[0m" % (sub_dom,dom,unic_ip)

if __name__ == '__main__':
if os.path.isfile(domain_file):
domain_index(domain_file)
for line in file(domain_file).readlines():
if line == '\n':
continue
if line[-1] == '\n':
line=line[:-1]
if illegal_list:
for g in illegal_list:
try:
line.split()[0].index(g)
except:
add_record(line.split()[0],line.split()[1])
else:
add_record(line.split()[0],line.split()[1])
else:
print "\033[31m[Error]: Domain file [ %s ]not found...\033[0m" % domain_file
sys.exit()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: