The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Editing DNS Zone Files Using Perl

Discussion in 'Bind / DNS / Nameserver Issues' started by aarondwyer, May 10, 2006.

  1. aarondwyer

    aarondwyer Well-Known Member

    Joined:
    Mar 26, 2005
    Messages:
    73
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    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
     
  2. bhd

    bhd Well-Known Member

    Joined:
    Sep 20, 2003
    Messages:
    149
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    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!
     
  3. abrender

    abrender Active Member

    Joined:
    Dec 5, 2002
    Messages:
    37
    Likes Received:
    0
    Trophy Points:
    6
    Don't forget that you'll want to update the serial number as well...
     
  4. aarondwyer

    aarondwyer Well-Known Member

    Joined:
    Mar 26, 2005
    Messages:
    73
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    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
     
  5. aarondwyer

    aarondwyer Well-Known Member

    Joined:
    Mar 26, 2005
    Messages:
    73
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    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
     
  6. aarondwyer

    aarondwyer Well-Known Member

    Joined:
    Mar 26, 2005
    Messages:
    73
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    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
     
  7. erick_paper

    erick_paper Well-Known Member

    Joined:
    Apr 19, 2005
    Messages:
    245
    Likes Received:
    0
    Trophy Points:
    16
    Actually, where is the serial number? I don't see it inside the *.db file?
     
Loading...

Share This Page