Automatically re-configure secondary Bind server to primary
With this script, you will able to re-configure a DNS server running on Bind9 from a secondary to a primary role :
- For “secondary”, I talk about the server that hosts the slave zones (read only)
- For “primary”, I talk about the server that hosts the master zones (writable)
Before running this Python script, you have to configure the variable bind_conf_path.
The script follows these steps :
- create a backup of the current file named.conf
- replace each line of the named.conf to configure all zones as master
- save the result in the file named.conf-master
- replace the named.conf file by named.conf-master
- reload zone file using the command rndc reload
import shutil
import re
#Define variables
bind_conf_path = "/etc/bind/"
named_src_slave = str(bind_conf_path)+"named.conf"
named_bkp_slave = str(bind_conf_path)+"named.conf-secondary"
named_dst_mastr = str(bind_conf_path)+"named.conf-master"
shutil.copyfile(named_src_slave, named_bkp_slave)
#Regex to get the lines containing the master ip address for the zone
regexp = re.compile(r'masters { \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}; };')
outfile = open(named_dst_mastr, 'w')
#Create a new named.conf with master zone instead of slave zone
with open(named_bkp_slave) as f:
for line in f:
if line.strip() == "type slave;":
line = "\ttype master;\n"
elif regexp.search(line.strip()) is not None:
line = ''
outfile.write(line)
outfile.close()
#Replace the named.conf with the new one that contains the dns zone configured as master
shutil.copyfile(named_dst_mastr,named_src_slave)
#Reload Bind zone file
from subprocess import call
call(["rndc", "reload"])
Automatically re-configure secondary Bind server to primary
