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()