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()
相关文章推荐
- 利用Dnspod api批量更新添加DNS解析【python脚本】
- 利用Dnspod api批量更新添加DNS解析【python脚本】 - 推酷
- python实现dnspod自动更新dns解析的方法
- Python脚本,自动更新host,添加or更新本机hosts
- [python]基于BeautifulSoup解析百度贴吧water_maker(谁最水)的脚本,练手打趣之作[更新0.3]
- python实现dnspod自动更新dns解析的方法
- python 脚本 通过解析 DDL 更新PDM
- shell 脚本 更新或者添加host ,并且增加hostname映射到hosts (修改)
- zabbix自动添加删除主机的python脚本
- 解析百度搜索结果页面的python脚本(Linux/Win都可以运行)
- shell 脚本 更新或者添加host
- Python 实现在对一个目录下所有文件,指定某一行之后添加内容(批处理脚本)
- ddnslib.py - 更新adsl用户外网ip到动态域名网站no-ip.com,并发送新ip到指定邮箱的python脚本
- LAMP自动化安装脚本更新(添加了mcrypt)
- Python中DNS解析的几个方法
- [Python]网络爬虫(八):糗事百科的网络爬虫(v0.3)源码及解析(简化更新)
- android4.0 升级中python脚本解析ota_from_target_files<一>
- python用于url解码和中文解析的小脚本
- DNS更新 与 解析(摘自互联网)
- windows下修改hosts文件,添加固定的DNS解析