class DomainHandler(object):
def __init__(self):
pass
def exec_cmd(self,cmd):
res = Popen(cmd, shell=True, stdout=PIPE)
ret = res.communicate()[0].decode('utf-8')
return ret.strip()
1.
2.
3.
4.
5.
6.
7.
8.
下面以添加A记录为例。
添加字典对应函数入口:
dic ={'1':DomainHandler().add,'2':DomainHandler().mod,'3':DomainHandler().delete}
tag =True
while tag:
print(''' 1.增加 2.修改 3.删除 q.退出 ''')
choice = input('\033[1;42m输入选项:\033[0m').strip()
if not choice:
continue
if choice =='q':
break
if choice in dic:
dic[choice]()
else:
print('\033[31m选项不存在\033[0m')
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
添加记录的入口函数:
def add(self):
self.domain_info()
while tag:
self.domain_id= input('\033[1;42m输入域名ID:\033[0m').strip()
if self.domain_id=='q':
break
if not self.domain_idornot self.domain_id.isdigit():
print('\033[31merror id\033[0m')
continue
self.sub_domain= input('\033[1;42m子域名[@或*等]:\033[0m').strip()
self.record_type= input('\033[1;42m类型[A或CNAME]:\033[0m').strip()
self.address= input('\033[1;42m记录值(ip或域名):\033[0m').strip()
if not self.sub_domainornot self.record_typeornot self.address:
print('\033[31m参数不能为空\033[0m')
continue
self.add_Arecord(self.domain_id,self.sub_domain,self.record_type,self.address)
if self.domain_id=='q'or self.record_type=='q'or self.address=='q':
self.tag=False
break
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
获取域名信息:
def domain_info(self):
cmd ='curl -s https://dnsapi.cn/Domain.List -d "login_token=391845,92f408bb5343e&format=json"'
data = json.loads(self.exec_cmd(cmd))
print(data)
for item in data['domains']:
print('%s:%s'%(item['name'], item['id']))
1.
2.
3.
4.
5.
6.
添加记录:
def add_Arecord(self,domain_id,sub_domain,record_type,address):
print(domain_id,sub_domain,record_type,address)
cmd2 ="curl -s -X POST https://dnsapi.cn/Record.Create -d 'login_token=391845,92f408bb5343e&format=json&domain_id={0}&sub_domain={1}&record_type={2}&record_line_id=0&value={3}'".format(
domain_id, sub_domain, record_type, address)
r = json.loads(self.exec_cmd(cmd2))
print(r['status']['message'])