Import DNS zone file in Bind with Python

Import DNS zone file in Bind with Python

I have written this procedure to help you on importing DNS zone in an existing Bind server. In the following procedure, I suppose the file to import have the “.dns” extension. To import these DNS zone files, you have to follow these steps :

  • change the content of the zone files (SOA and NS records)
  • update the named.conf file on both master and slave dns servers
  • load the zones

All the following sed command can be run without the option “-i” for testing without writing the file.

To update both SOA and NS records, you can use the sed command :

  • create a temporary folder : mkdir /tmp/zone_files
  • copy the zone files to the folder created above
  • change the SOA : sed -i 's/ns1.domain.old. hostmaster.domain.old./ns1.domain.new. hostmaster.domain.new./g' *.dns
  • delete the old NS entries : sed -i '/NS/d' *.dns
  • add the first new NS record : sed -i 's/\t\t\t\t)/\t\t\t\t)\n\t\t\tNS      ns2.domain.new./' *.dns
  • add another NS record if applied : sed -i 's/\t\t\t\t)/\t\t\t\t)\n\t\t\tNS      ns3.domain.new.' *.dns
  • backup your named.conf file : cp /etc/bind/named.conf /etc/bind/named.conf.bkp
  • create the import_zone.py with this content :

  • run the script and check the content of the named.conf : python import_zone.py
  • reload the dns zones : rndc reload
  • You can now change the named.conf on the secondary dns servers with the following script (change the ip address with your own master dns server ip address) :

  • reload the dns zones : rndc reload

Do not hesitate to leave a comment if you have any problem or questions with this procedure.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Social Media Auto Publish Powered By : XYZScripts.com
%d bloggers like this: