有一堆在dnspod的域名要批量修改解析,虽然dnspod网页上可以批量修改,但是这些域名在不同的dnspod账户中,后续还会频繁修改解析,考虑到后续的工作量,还是写个脚本省事一点。dnspod的api还是比较简单的,api文档地址: http://www.dnspod.cn/docs/index.html
python 脚本:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'will'

import sys,requests,json

class Dnspod:
    def __init__(self,token):
        self.token = token

    def getDomainList(self):
        postdata = {'login_token': self.token,'format':'json'}
        with requests.session() as start:
            login = start.post('https://dnsapi.cn/Domain.List',postdata)
            return login.text

    def getSub(self,domain):
        if len(domain.split('.')[0:-2]) == 0:
            return '@',str(domain)
        else:
            sub = ''
            for i in domain.split('.')[0:-2]:
            sub += i+ '.'
            return sub[0:-1], domain.split('.')[-2]+'.'+domain.split('.')[-1]

    def getRecord(self,domain):
        sub,root=self.getSub(domain)

        postdata = {'login_token': self.token,'format':'json','domain':root,'sub_domain':sub}
        with requests.session() as start:
            login = start.post('https://dnsapi.cn/Record.List',postdata)
            return json.loads(login.text)['records'][0]['id'],json.loads(login.text)['records'][0]['value']

    def modRecord(self,domain,ip,record_type,record_line):
        sub,root = self.getSub(domain)
        record_id,value = self.getRecord(domain)
        
        postdata = {'login_token': self.token,'format':'json','domain':root,'sub_domain':sub,'record_id':record_id,'value':ip,'record_type':record_type,'record_line':record_line}
        with requests.session() as start:
            login = start.post('https://dnsapi.cn/Record.Modify',postdata)
            if json.loads(login.text)['status']['code'] == '1':
                return 'success'
            else:
                return json.loads(login.text)

    def change(self,domain,ip,record_type,record_line):
       sub,root = self.getSub(domain)
       all_domains=[]
       for domain_name in  json.loads(self.getDomainList())['domains']:
           all_domains.append(domain_name['name'])
       if root not in all_domains:
           print '账户中未找到该域名'
           sys.exit(0)
       print '域名: '+ domain +  ' 当前解析: ' + str(self.getRecord(domain)[1]) +  ' 更改解析为: ' + ip + ' 记录类型: ' + record_type + ' 线路: ' + record_line  +' 状态: ' +str(self.modRecord(domain,ip,record_type,record_line))
if __name__ == '__main__':

    token="12345,7d9s9d00w8s7d7dko20394l04r23"

    Dnspod(token).change('www.52os.net','8.8.8.8','A','默认')