Python脚本:修改Bind域名解析文件

近由于工作需要,经常需要修改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)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/71cb709dd098af12cbdd1431de67ca4c.html