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

这篇博客介绍了一个Python脚本,旨在简化Bind域名解析配置的管理。脚本能自动添加IN A和IN PTR记录,删除指定记录,查看解析记录,并在修改后自动更新配置文件的同步串码。它还包括交互式菜单和提示,方便用户操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近由于工作需要,经常需要修改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 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值