近由于工作需要,经常需要修改Bind的域名解析配置文件。由于最近一直在学Python所以就用Python写了一个脚本。具体实现功能如下:
1,可以在正向解析和反向解析配置文件中,自动添加’IN A’ 和 ‘IN PTR’ 记录;
2,可以删除指定的记录,包括正向解析和反向解析;
3,可以查看正向解析记录和反向解析记录;
4,在修改配置文件后可以自动更新同步串码(Serial);
5,每项操作均配置有菜单选项和相关提示。
全部脚本如下:
#/usr/bin/env python
#Language: Python
#Author:CMZSteven
#Last Change:2017-03-06
#Version:1.1
#Bug fix:CMZSteven
import time
import sys
#配置解析文件所在的目录
config_path = "/var/named/"
#配置正向查找配置文件
forward_resolution_conf = config_path + "named.abc.com"
#配置反向查找配置文件
reverse_parsing_conf = config_path + "named.10.0.0"
#配置IP网段
prefix_of_ip = "10.0.0."
#配置域名,最后面一定要有一个‘.’
postfix_of_host = ".abc.com."
def update_single_sn(conf_file):
date = time.strftime('%Y%m%d',time.localtime(time.time()))
file=open(conf_file,"r+")
file.next()
old_sn = file.next().split()[6]
new_sn = ""
if str(int(old_sn)/100) == date:
new_sn = str(int(old_sn) - int(date) * 100 + int(date) * 100 + 1)
else:
new_sn = str(int(date) * 100 + 1)
file.seek(0,0)
f = file.read()
f = f.replace(old_sn, new_sn)
file.seek(0,0)
file.write(f)
file.close()
print '\033[36;1mThe current\033[33;1m%s\033[36;1m \'sn is:\033[31;1m %s\033[0m' % (conf_file,new_sn)
def append_single_record(conf_file,record):
conf = open(conf_file,"a")
conf.write(record)
conf.close()
print '\033[36;1mThe record which append into \033[33;1m%s\033[36;1m is \033[32;1msuccessful!\033[0m' % conf_file
def get_host():
while True:
host = raw_input("\033[32;1mPlease input the host name(input 'exit' to quit):\033[0m").strip()
if host.isalnum() == False:
print "\033[31;1mThe name you input is contained illegal characters!\033[0m"
continue
else:
return host
def get_IP():
while True:
ip = raw_input("\033[32;1mPlease input the last part of host IP(input 'exit' to quit):\033[0m").strip()
if ip == "exit":
return ip
elif ip.isdigit():
if int(ip)>0 and int(ip)<255:
return ip
else:
print "\033[31;1mThe number you input is out of IP's range!\033[0m"
continue
else:
print "\033[31;1mThe number you input is not a number!\033[0m"
continue
def is_confirm(confirm_message):
continue_input = raw_input("\033[36;1m" + confirm_message +"\033[31;1m(y/n, yes/no)\033[0m").strip().lower()
if continue_input == 'y' or continue_input == 'yes':
return True
else:
return False
def get_full_records(host,ip):
full_ip = prefix_of_ip + ip
full_host = host + postfix_of_host
print '\033[36;1mThe DNS record info is:\033[0m'
print '\033[31;1mHost:\033[33;1m%s\033[0m' % full_host
print '\033[31;1mIP:\033[33;1m%s\033[0m' % full_ip
forward_resolution_record = host + "\t"*4 + "IN A \t" + full_ip + "\n"
reverse_parsing_record = ip + "\tIN PTR\t \t" + full_host + "\n"
records = {"forward_resolution":forward_resolution_record, "reverse_parsing":reverse_parsing_record}
return records
def append_process(host, ip):
records = get_full_records(host,ip)
append_single_record(forward_resolution_conf, records['forward_resolution'])
append_single_record(reverse_parsing_conf, records['reverse_parsing'])
def update_sn_process():
update_single_sn(forward_resolution_conf)
update_single_sn(reverse_parsing_conf)
def print_same_record_tips(record,same_type,conf_file,line_num):
print "\033[36;1mThere is have the same " + same_type + " in \033[31;1m%s\033[36;1m which you want to append! The record line num: is: \033[31;1m%s\033[0m" % (conf_file,line_num)
print "\033[36;1mRecord info:\033[0m"
print "\033[33;1m" + record + "\033[0m"
def delete_certain_record(conf_file, line_num):
conf = open(conf_file, "r+")
lines = conf.readlines()
conf.close()
conf = open(conf_file, "r+")
conf.truncate()
conf.close()
conf = open(conf_file,"r+")
del lines[line_num]
conf.writelines(lines)
conf.close()
def is_same_record(conf_file,host,ip,host_sec_num,ip_sec_num):
conf = open(conf_file)
line_num = 1
line_num_list = []
for line in conf:
sections = line.split()
host_in_conf = ""
ip_in_conf = ""
if len(sections) == 4:
host_in_conf = sections[host_sec_num].strip()
ip_in_conf = sections[ip_sec_num].strip()
if(host_in_conf == host and len(host_in_conf) == len(host)) and (ip_in_conf == ip and len(ip_in_conf) == len(ip)):
print_same_record_tips(line, "Record", conf_file, line_num)
line_num_list.append(line_num -1)
else:
if host_in_conf == host and len(host_in_conf) == len(host):
print_same_record_tips(line, "Host", conf_file, line_num)
line_num_list.append(line_num -1)
elif ip_in_conf == ip and len(ip_in_conf) == len(ip):
print_same_record_tips(line, "IP", conf_file, line_num)
line_num_list.append(line_num -1)
line_num += 1
if len(line_num_list) > 0:
print "\033[31;1m" + "-"*80 + "\033[0m"
if is_confirm("Do you want delete duplicate DNS Record Automactically?") == True:
line_num_list.reverse()
for i in line_num_list:
delete_certain_record(conf_file,i)
return False
else:
return True
else:
return False
def is_operate_append_process(host,ip):
full_ip = prefix_of_ip + ip
full_host = host + postfix_of_host
is_operate = True
if is_same_record(forward_resolution_conf,host,full_ip,0,3) == False:
is_operate = True
else:
is_operate = False
if is_same_record(reverse_parsing_conf,full_host,ip,3,0) == False:
is_operate = True
else:
is_operate = False
return is_operate
def append_DNS_record():
modify_num = 0
while True:
host = get_host()
if host == 'exit':
break
ip = get_IP()
if ip == 'exit':
break
if is_operate_append_process(host,ip) == True:
append_process(host, ip)
modify_num += 1
else:
print "\033[31;1m" + "-"*80 + "\033[0m"
print "\033[31;1mThe DNS config file have one or more duplicate record, please processing these record mannually and try again!\033[0m"
print "\033[31;1m" + "-"*80 + "\033[0m"
if is_confirm("Do you want to input another DNS record?") == False:
break
if modify_num > 0:
update_sn_process()
show_main_menu()
def list_dns_record(conf_file, key_words):
print "\033[36;1m" + "-" * 80 + "\033[0m"
print "\033[33;1mThe config file " + conf_file + " records:\033[0m"
print "\033[36;1m" + "-"*80 + "\033[0m"
conf = open(conf_file)
line_num = 1
line_num_list = []
for line in conf:
if line.find(key_words) > 0:
print "\033[31;1m" + str(line_num) + ")\033[0m\t" + line,
line_num_list.append(line_num)
line_num += 1
print "\033[33;1mThe End of " + conf_file + "\033[0m\n"
return line_num_list
def menu_printer(menu_title, menu_list = []):
print "\033[36;1m" + "-"*80 + "\033[0m"
print "\033[32;1m" + menu_title + "\033[0m"
print "\033[33;1mMenu:\033[0m"
num = 1
for item in menu_list:
print "\033[31;1m" + str(num) + ".\033[36;1m" + item + "\033[0m"
num += 1
print "\033[36;1m" + "-"*80 + "\033[0m"
def show_all_dns_record():
while True:
menu_item = ['Print forward resolution record','Print reverse parsing record','Print All','Exit']
menu_printer("Print DNS Record", menu_item)
select_menu_num = check_menu_select(menu_item)
if select_menu_num == "1":
list_dns_record(forward_resolution_conf,'IN A')
elif select_menu_num == "2":
list_dns_record(reverse_parsing_conf,'IN PTR')
elif select_menu_num == "3":
list_dns_record(forward_resolution_conf,'IN A')
list_dns_record(reverse_parsing_conf,'IN PTR')
else:
break
show_main_menu()
def check_menu_select(menu_item = []):
select_menu_num = 0
while True:
select_menu_num = raw_input("\033[32;1mPlease input the number of Menu:\033[0m").strip()
if select_menu_num.isdigit() == False:
print "\033[31;1mYour input is not a number, please try again!\033[0m"
continue
elif int(select_menu_num) <= 0 or int(select_menu_num) > len(menu_item):
print "\033[31;1mYour input should be between 1 to " + str(len(menu_item)) + ", please try agiain!\033[0m"
continue
else:
break
return select_menu_num
def deleted_record_num_exist(conf_file, key_words):
while True:
line_num_list = list_dns_record(conf_file, key_words)
delete_record_num = raw_input("\033[32;1mPlease input the record line number which you want to delete(input 'exit' to quit):\033[0m").strip()
if delete_record_num == 'exit':
break
is_have_num_in_list = False
for num in line_num_list:
if int(delete_record_num) == num:
is_have_num_in_list = True
if is_have_num_in_list == True:
delete_certain_record(conf_file, int(delete_record_num) -1)
list_dns_record(conf_file, key_words)
print "\033[33;1m" + "*"*80 + "\033[0m"
print "\033[31;1m**The record you specified is deleted, Please check out the Record list above!**\033[0m"
print "\033[33;1m" + "*"*80 + "\033[0m"
break
else:
print "\033[33;1m" + "*"*80 + "\033[0m"
print "\033[31;1mThe record line number you input is not exist, Please check the list below!\033[0m"
print "\033[33;1m" + "*"*80 + "\033[0m"
continue
def delete_dns_record():
while True:
menu_item = ['Delete forward resolution record','Delete reverse parsing record','Exit']
menu_printer("Delete DNS Record", menu_item)
select_menu_num = check_menu_select(menu_item)
if select_menu_num == "1":
deleted_record_num_exist(forward_resolution_conf, 'IN A')
update_single_sn(forward_resolution_conf)
elif select_menu_num == "2":
deleted_record_num_exist(reverse_parsing_conf, 'IN PTR')
update_single_sn(reverse_parsing_conf)
else:
break
show_main_menu()
def show_main_menu():
menu_item = ['Append DNS records','Delete DNS records','Print DNS records','Exit']
menu_printer("DNS Modfiy Program",menu_item)
select_menu_num = check_menu_select(menu_item)
if select_menu_num == "1":
append_DNS_record()
elif select_menu_num == "2":
delete_dns_record()
elif select_menu_num == "3":
show_all_dns_record()
else:
sys.exit(0)
if __name__ == '__main__':
show_main_menu()