Community Forums
Connect with us on LinkedIn
Community Notice
+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Member This forum account has been confirmed by cPanel staff to represent a vendor. aarondwyer's Avatar
    Join Date
    Mar 2005
    Location
    Brisbane
    Posts
    72

    Default Editing DNS Zone Files Using Perl

    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
    http://backup-smart.com Automatically And Safely Backup All Your cPanel Websites To Your Local Computer
    Free 7 Day Trial of this new software. MAC, PC, Linux

  2. #2
    bhd
    bhd is offline
    Member
    Join Date
    Sep 2003
    Location
    JNB ZA
    Posts
    142

    Default

    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. #3
    Member
    Join Date
    Dec 2002
    Posts
    39

    Default

    Don't forget that you'll want to update the serial number as well...

  4. #4
    Member This forum account has been confirmed by cPanel staff to represent a vendor. aarondwyer's Avatar
    Join Date
    Mar 2005
    Location
    Brisbane
    Posts
    72

    Default

    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
    http://backup-smart.com Automatically And Safely Backup All Your cPanel Websites To Your Local Computer
    Free 7 Day Trial of this new software. MAC, PC, Linux

  5. #5
    Member This forum account has been confirmed by cPanel staff to represent a vendor. aarondwyer's Avatar
    Join Date
    Mar 2005
    Location
    Brisbane
    Posts
    72

    Default

    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
    http://backup-smart.com Automatically And Safely Backup All Your cPanel Websites To Your Local Computer
    Free 7 Day Trial of this new software. MAC, PC, Linux

  6. #6
    Member This forum account has been confirmed by cPanel staff to represent a vendor. aarondwyer's Avatar
    Join Date
    Mar 2005
    Location
    Brisbane
    Posts
    72

    Default

    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
    http://backup-smart.com Automatically And Safely Backup All Your cPanel Websites To Your Local Computer
    Free 7 Day Trial of this new software. MAC, PC, Linux

  7. #7
    Member
    Join Date
    Apr 2005
    Posts
    246

    Default

    Actually, where is the serial number? I don't see it inside the *.db file?

Similar Threads & Tags
Similar threads

  1. Editing DNS Zone
    By issu in forum cPanel and WHM Discussions
    Replies: 2
    Last Post: 08-26-2011, 08:18 AM
  2. Editing zone files in shell
    By meeven in forum cPanel and WHM Discussions
    Replies: 5
    Last Post: 09-21-2008, 01:37 AM
  3. editing dns zone manually.
    By p_s_p in forum cPanel and WHM Discussions
    Replies: 4
    Last Post: 12-26-2005, 06:52 AM
  4. Problem when Editing a DNS Zone
    By Marty in forum cPanel and WHM Discussions
    Replies: 6
    Last Post: 05-13-2002, 10:06 PM
  5. Editing DNS Zone - getting frustrated
    By AlaskanWolf in forum cPanel and WHM Discussions
    Replies: 7
    Last Post: 03-13-2002, 06:16 PM
Linkedin       Facebook       Twitter       RSS       Flickr       YouTube