dnspod api调用脚本
[root@localhost dnspod]# cat dnspod.sh #!/bin/bash function menu() { clear echo "*********************Dnspod************************" echo "* 1,将加速乐cname批量修改到dnspod *" echo "* 2, 将ip批量修改到dnspod *" echo "* 3, 单个域名修改 *" echo "* 4, 退出 *" echo "***************************************************" read -p "请输入编号: " number read -p "请输入D令牌: " number2 } while true do menu case $number in 1) python /python/saltnew/dnspod/dnspod.py batch_jiasule $number2 read -p "回车继续: ";; 2) python /python/saltnew/dnspod/dnspod.py batch_dnspod $number2 read -p "回车继续: ";; 3) python /python/saltnew/dnspod/dnspod.py none $number2 read -p "回车继续: ";; *) exit;; esac done
root@localhost dnspod]# cat dnspod.py #!/bin/python #-*- coding: utf-8 -*- import urllib import urllib2 import os,sys,json,demjson,fileinput #处理post请求 def post(url,data): req = urllib2.Request(url) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json", "User-Agent": "dnspod-python/0.01 (im@chuangbo.li; DNSPod.CN API v2.8)"} data = urllib.urlencode(data,headers) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) response = opener.open(req, data) return response.read() #主函数 def main(batch,login_code): #获取域名列表 posturlDomain='https://dnsapi.cn/Domain.List' dataDomain={'login_email':'test@test.com','login_password':'123456','format':'json','login_code':login_code} Domain={} records={} number=0 numbertwo=0 batchlist={} jiasulelist={} dnspodlist={} for i in demjson.decode(str(post(posturlDomain,dataDomain)))[u'domains'][:]: number+=1 Domain[number]={str(i[u'name']):str(i[u'id'])} for key,value in Domain.items(): print key,value numb=input("please input number: ") domain_number=Domain[numb].values()[0] #获取二级域名列表 posturlrecordlist='https://dnsapi.cn/Record.List' datarecordlist={'login_email':'test@test.com','login_password':'123456','format':'json','domain_id':domain_number,'login_code':login_code} for i in demjson.decode(str(post(posturlrecordlist,datarecordlist)))[u'records'][:]: numbertwo+=1 batchlist[i[u'name']]=str(i[u'id']) records[numbertwo]={str(i[u'name']):str(i[u'id'])} #批量恢复 if batch == "batch_jiasule": for line in fileinput.input("/python/saltnew/dnspod/jiasule.txt"): name=line.strip('\n').split(' ')[0] cname=line.strip('\n').split(' ')[1] records_id=batchlist[name] jiasulelist[name]={cname:records_id} posturlmodify_batch='https://dnsapi.cn/Record.Modify' datamodify_batch={'login_email':'test@test.com','login_password':'123456','format':'json','domain_id':domain_number,'sub_domain':name,'record_type':'CNAME','record_line':u'默认'.encode("utf8"),'value':cname,'record_id':records_id,'login_code':login_code} print post(posturlmodify_batch,datamodify_batch) sys.exit(1) if batch == "batch_dnspod": for line in fileinput.input("/python/saltnew/dnspod/dnspod.txt"): name=line.strip('\n').split(' ')[0] ip=line.strip('\n').split(' ')[1] records_id=batchlist[name] dnspodlist[name]={ip:records_id} posturlmodify_dns='https://dnsapi.cn/Record.Modify' datamodify_dns={'login_email':'test@test.com','login_password':'123456','format':'json','domain_id':domain_number,'sub_domain':name,'record_type':'A','record_line':u'默认'.encode("utf8"),'value':ip,'record_id':records_id,'login_code':login_code} print post(posturlmodify_dns,datamodify_dns) sys.exit(1) #单个修改 for key,value in records.items(): print key,value numb2=input("please input number: ") records_number=records[numb2].values()[0] records_name=records[numb2].keys()[0] record_type=raw_input("请输入记录类型: ") record_ip=raw_input("请输入记录值: ") #修改某个二级域名的记录类型与记录值 posturlmodify='https://dnsapi.cn/Record.Modify' datamodify={'login_email':'test@test.com','login_password':'123456','format':'json','domain_id':domain_number,'sub_domain':records_name,'record_type':record_type,'record_line':u'默认'.encode("utf8"),'value':record_ip,'record_id':records_number,'login_code':login_code} print post(posturlmodify,datamodify) if __name__ == '__main__': main(sys.argv[1],sys.argv[2])
dnspod.txt testtest 2.2.2.2 testtest2 1.1.1.1 jiasule.txt testtest 767b885d566c6111.cdn.jiashule.com. testtest2 b97c167111aba5dc.cdn.jiashule.com.