Editing DNS Zone Files Using Perl

aarondwyer

Well-Known Member
Verifed Vendor
Mar 26, 2005
73
0
156
Brisbane
cPanel Access Level
Root Administrator
Hi

I want to edit all my DNS .db files to add in a secondary mx.

I know there are some Perl gurus around here that could help me with a simple Perl one liner to add 1 line to the end of each .db file.

This is the line I want to add

@ 14400 IN MX 10 seconaryserver.com.

So it'll be something like this

cd /var/named

then

perl -pi.bak -e "[here's the bit where I have no idea what to put]" *.db

then restart DNS

/etc/init.d/named restart


Thanks
Aaron
 

bhd

Well-Known Member
Sep 20, 2003
149
2
166
JNB ZA
cPanel Access Level
Root Administrator
Something like this should do the job:

Code:
#!/usr/bin/perl
# 1. Open all zone files in /var/named
# 2. Add a line of text beneath all lines "mail IN" in each zone

use strict;

my $whatever = 'Text for line to add goes here';

opendir DIR, '/var/named';
my @d = readdir DIR;


foreach my $f (@d){
   if (length $f > 2){
	  open IN, "</var/named/$f";
	  my @zone = <IN>;
	  close IN;

      open OUT, ">/var/named/$f";

	  foreach my $line (@zone){
	    $line =~ s/\n//g;
	    print OUT $line . "\n";
	    if ($line =~ m/mail IN/){
	      print OUT $whatever . "\n";
	    }
	  }
        close OUT;
        print "$f updated\n";
  }

}
Make sure you make a backup of your zone files first!
 

aarondwyer

Well-Known Member
Verifed Vendor
Mar 26, 2005
73
0
156
Brisbane
cPanel Access Level
Root Administrator
Thanks for the solution. Works like a charm.

I added in the serial number as well.

Bugger that my DNS .db files are in 2 different formats. Untouched files are different to files that have been edited by WHM.

Just when I thought I had it made.

I'll post back the final solution once I've got it sorted.

Aaron
 

aarondwyer

Well-Known Member
Verifed Vendor
Mar 26, 2005
73
0
156
Brisbane
cPanel Access Level
Root Administrator
With the advent of 2 file formats of .db files.

My method of changing the serial number doesn't work across both.

So I need to look for something different. I have very little knowledge of sed.

What's the sed command for finding a 10 digit number.?

Like this 2005051101

So to change the code of

if ($line =~ m/mail IN/){

to find a 10 digit number (aka the serial number)

Thanks
Aaron
 

aarondwyer

Well-Known Member
Verifed Vendor
Mar 26, 2005
73
0
156
Brisbane
cPanel Access Level
Root Administrator
Thanks to choon and WHT-AU

This is to tickle the serial number, had a slight issue with this not quite working on only a handful of .db files where it substituted the date code in for a @

I don't know anything about SED so just edited those by hand.

At least I have this for future use of adjusting the serial number on DNS entries.

Code:
#!/bin/sh

newsoa=`date +%Y%m%d%H`

for i in `ls /var/named|grep '\.db$'`; do
	if [ -f "/var/named/${i}" ] ; then
		currentsoa=`grep -E '[0-9]{10}' /var/named/${i}|awk '{print $1}'`
		echo -n "Start to replace ${i} SOA ${currentsoa} to ${newsoa} ... "
		# uncomment below after the above result is what you want
		# sed -i -e "s#${currentsoa}#${newsoa}#" /var/named/${i}
		echo "done."
	fi
done
Thanks
Aaron