Slave DNS auto-record creator perl script

rhoekman

Verified User
Joined
Jan 3, 2004
Messages
250
Location
The Netherlands
Ok, before you just copy past and run this you need to know this is still work in progress!

Goal:

Add newly created records to named.conf on secondary nameserver automagicly.

Problem and my question to Perl programmers out there:

It works the first time but after that it will append all records again to the named.conf on the secondary so I need help here!

How does it work:

You copy the named.conf from the primairy dns to an user directory with ftp access and call it secondary.conf. This perl script (on the secondary nameserver) logs in via ftp, looks at the file and will make the changes to the secondary named.conf. Then deletes the one on the primairy (the one in the user directory). After that a message will be sent to the admin to review the newly added entries and commence a ndc reload.


<--script-->
Code:
#!/usr/bin/perl -w

use Net::FTP;
use Mail::Mailer;

$from_address = "root\@domain.com";
$to_address = "admin\@domain.com";
$hostname = "ftp.domain.com";
$username = "yourloginnname";
$password = "yourpassword";
$filename = "/secondary.conf";
$local = "/tmp/secondary.conf";
$named = "/etc/namedb/named.conf";
$ftp = Net::FTP->new($hostname);
$ftp->login($username, $password);
$ftp->get($filename, $local) || die "Nothing to do!\n\n";;
$ftp->delete($filename) || die "Can't delete! Fix perms!\n\n";
$ftp->quit;

open(OUT,"$local");
@array = <OUT>;
open(NAMED,"$named");
@conf = <NAMED>;
$string = join(":", @conf);
my $i = 0;
while ($array[$i]) {
        if ($array[$i] =~ /zone/) {
                        if ($string !~ /$array[$i]/) { 
                        open(NAMED,">>$named");
                        print NAMED "\n$array[$i]";
                        $list[$i] = $array[$i];
                        $i++;
                                while($array[$i]) {
                                        print NAMED $array[$i];
                                        $list[$i] = $array[$i];
                                        $i++;
                                }
                        }
        }
        close NAMED;
        $i++;
}

`rm $local`;

$list2 = join("",@list);
if ($list2) {
$subject = "New changes to secondary DNS!";
$body = "Hi,\n\n\tThe named.conf for the secondary DNS has
 been updated with the following new domain entries:\n\n$list2\n\n
Remember
 to look over the changes and then run \"ndc reload\"";

$mailer = Mail::Mailer->new("sendmail");
$mailer->open({ From    => $from_address,
                To      => $to_address,
                Subject => $subject,
              })
    or die "Can't open: $!\n";
print $mailer $body;
$mailer->close();

}

<-- /script -->
 
Last edited:
rhoekman,

Search this forum. There's already a solution (it seems, very similar to yours) in beta testing now.

We are using it on 3 servers now and it's working well.

They've solved the dupe issue. (Well, kinda.)

=C=
 
Back
Top