码迷,mamicode.com
首页 > 其他好文 > 详细

dnspod 批量添加记录

时间:2018-03-16 17:23:36      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:批量添加   post   ftime   写入   pod   utf-8   led   records   域名   

#!/usr/bin/python
#-*- coding: utf-8 -*-
import os,requests,json
import re,xlsxwriter,time
import xlrd
#curl -X POST https://dnsapi.cn/Info.Version -d ‘login_token=LOGIN_TOKEN&format=json‘


#时间
date=time.strftime("%Y-%m-%dT%H-%M-%S", time.localtime())
#文件名 
name=域名筛选-{0}.xlsx.format(date)

url=https://dnsapi.cn/Info.Version
token=
domail_name=daddymami.net

data={login_token:{0}.format(token),format:json}
r = requests.post(url,data=data)
if r.json()[status][code] != 1:
    print(登录失败,检查网站,token,id)
    exit(0)
else:
    print(登录成功,正在执行脚本)


url=https://dnsapi.cn/Domain.List

data={login_token:{0}.format(token),format:json,type:all}
r = requests.post(url,data=data)
json_data=r.json()



#域名名字和id
id_name=dict()
domain_name_all=json_data[domains]
for i in domain_name_all:
    id_name[str(i[name])]=i[id]

print(id_name[daddymami.net])


url=https://dnsapi.cn/Record.Create

ERROR=dict()
#指定文件位置
execl_file=‘‘
#指定页
table_name=‘‘
bk = xlrd.open_workbook(execl_file)
shxrange = range(bk.nsheets)
try:
    table = bk.sheet_by_name(table_name)
except:
    print ("no sheet in %s named %s"%(execl_file,table_name))

nrows = table.nrows
num=1

for row in range(1,nrows):
    
    host_value = table.cell_value(row,1)
    Record_type = table.cell_value(row,2)
    line_value = table.cell_value(row,3)
    Record_value = table.cell_value(row,4)
    MX = table.cell_value(row,5)
    if host_value != ‘‘:
        if MX == -:
            data={login_token:{0}.format(token),format:json,domain_id:id_name[domail_name],sub_domain:host_value,record_line:line_value,record_type:Record_type,value:Record_value}
            r = requests.post(url,data=data)
            output = open(data%s.json%date, w)
            ERROR[row]={"code":int(r.json()[status][code]),"host":str(host_value)}
            output.write(json.dumps(ERROR))
            num +=1
        else:
            data={login_token:{0}.format(token),format:json,domain_id:id_name[domail_name],sub_domain:host_value,record_line:line_value,record_type:Record_type,value:Record_value,mx:MX}
            r = requests.post(url,data=data)
            output = open(data%s.json%date, w)
            ERROR[row]={"code":int(r.json()[status][code]),"host":str(host_value)}
            output.write(json.dumps(ERROR))
            num +=1
        output.close( )

添加完成后,在做对比,看哪些没添加成功

#!/usr/bin/python
#-*- coding: utf-8 -*-
import os,requests,json
import re,xlsxwriter,time
import xlrd
#curl -X POST https://dnsapi.cn/Info.Version -d ‘login_token=LOGIN_TOKEN&format=json‘
#文件名
date=time.strftime("%Y-%m-%dT%H-%M-%S", time.localtime()) 
name={0}.xlsx.format(date)

def start_data():
    
    #登录测试
    url=https://dnsapi.cn/Info.Version
    token=
    domail_name=daddymami.net
    
    data={login_token:{0}.format(token),format:json}
    r = requests.post(url,data=data)
    if r.json()[status][code] != 1:
        print(登录失败,检查网站,token,id)
        exit(0)
    else:
        print(登录成功,正在执行脚本)
    
    #获取域名ID
    
    url=https://dnsapi.cn/Domain.List
    
    data={login_token:{0}.format(token),format:json,type:all}
    r = requests.post(url,data=data)
    json_data=r.json()
    
    
    
    #域名名字和id
    id_name=dict()
    domain_name_all=json_data[domains]
    for i in domain_name_all:
        id_name[str(i[name])]=i[id]
    
    print(id_name[daddymami.net])
    
    
    #获取域名记录
    url=https://dnsapi.cn/Record.List
    
    ERROR=dict()
    
    #对比dnspod与文档的记录是否一致
    
    #指定文件位置
    execl_file=‘‘
    #指定页
    table_name=‘‘
    
    bk = xlrd.open_workbook(execl_file)
    shxrange = range(bk.nsheets)
    try:
        table = bk.sheet_by_name(table_name)
    except:
        print ("no sheet in %s named %s"%(execl_file,table_name))
    
    nrows = table.nrows
    num=1
    domail_data=dict()
    dict_num=0
    for row in range(1,nrows):
        host_value = str(table.cell_value(row,1))
        Record_type = str(table.cell_value(row,2))
        line_value = str(table.cell_value(row,3))
        Record_value = str(table.cell_value(row,4))
        MX = str(table.cell_value(row,5))
        TTL = str(table.cell_value(row,6))
        
        if host_value != ‘‘:
            data={login_token:{0}.format(token),format:json,domain_id:id_name[domail_name],keyword:host_value}
            r = requests.post(url,data=data)
            num +=1
            print(host_value,int(r.json()[status][code]))
            if int(r.json()[status][code]) == 1:
                num=0
                for ii in r.json()[records]:
                    #由于len从1计数所以这里循环开始就加1
                    num+=1
                    if len(ii) > 0:
                        if str(ii[status]) == enabled and str(ii[name]) == host_value and str(ii[value]) == Record_value and str(ii[line]) == line_value and str(ii[type]) == Record_type:
                            break
                        else:
                            #如果循环的次数等于或等于json里的数据数量,那么意味着这条记录在dnspod不存在,加入到domail_data做记录
                            if num >= len(r.json()[records]) :
                                dict_num+=1
                                domail_data[dict_num]={host_value:host_value,Record_type:Record_type,line_value:line_value,Record_value:Record_value,MX:MX,TTL:TTL}
                                num=1
                            
            
            else:
                dict_num+=1
                domail_data[dict_num]={host_value:host_value,Record_type:Record_type,line_value:line_value,Record_value:Record_value,MX:MX,TTL:TTL}
                
            
    output = open(data%s.json%date, w)
    output.write(json.dumps(domail_data))
    output.close( )
    print(xlsx原始数据%sdata%s.json%(os.path.abspath(os.curdir)+"\\",date))
#为了避免xlsxwriter出现故障导致又要重新查询一次dnspod,这里直接写入到json文件做存储,然后在取出,后面出现故障可直接修改open文件名
start_data()
output = open(data%s.json%date)
domail_data = json.load(output)

#设置文件名
workbook=xlsxwriter.Workbook(name)
#设置宽度
worksheet=workbook.add_worksheet()
worksheet.set_column(A:A,20)
worksheet.set_column(B:B,22)
worksheet.set_column(C:C,20)
worksheet.set_column(D:D,15)
worksheet.set_column(E:E,15)
worksheet.set_column(F:F,15)

#生成报表
title = [u主机记录,u记录类型,u线路类型,u记录值,uMX优先,TTL]
worksheet.write_row(A1,title)
num = 2
for i in domail_data.keys():
    tmp=[domail_data[i][host_value],domail_data[i][Record_type],domail_data[i][line_value],domail_data[i][Record_value],domail_data[i][MX],domail_data[i][TTL]]
    worksheet.write_row(A{0}.format(num),tmp)
    num+=1

workbook.close()
#
print(执行完毕,文件路径+os.path.abspath(os.curdir)+"\\"+name)                        
                        
            
            

 

dnspod 批量添加记录

标签:批量添加   post   ftime   写入   pod   utf-8   led   records   域名   

原文地址:https://www.cnblogs.com/cainiaoit/p/8582680.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!