Subversion Repositories sysadmin_scripts

Rev

Rev 75 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#! /usr/bin/perl -w

use Sys::Syslog;

my $statusFile = '/opt/bin/updatedns.status';
my $template = '/opt/bin/updatedns.template';
my $nsupdate = '/usr/bin/nsupdate';
my $keyfile = '/opt/bin/keys/Kdyndd.net.+157+35226.key';

my $entries;

sub doUpdateNS {
   my ($hostname, $ip) = @_;
   my $nsupdateFileName = "/tmp/$hostname.nsupdate";
   open TEMPLATE, "<$template" or die "could not open template $template for read: $!\n";
   my $template = join( '', <TEMPLATE> );
   close TEMPLATE;
   $template =~ s/\{hostname\}/$hostname/gi;
   $template =~ s/\{ip\}/$ip/gi;
   open OUTPUT, ">$nsupdateFileName" or die "Could not create $nsupdateFileName: $!\n";
   print OUTPUT $template;
   close OUTPUT;
   return `$nsupdate -k $keyfile $nsupdateFileName`;
}



my $hostname = shift;
my $realIP = shift;

print "$hostname,$realIP\n";

$realIP =~ m/^([\d.]+)/;
$realIP = $1;

openlog('updatedns', 'cons,pid', 'user');

die "You must send hostname with command\n" unless $hostname;

if ( $hostname !~ m/[a-z0-9-]\.dyndd\.net/ ) {
   syslog( 'warning', "Attempt to set incoming server name to invalid host [$hostname]");
   exit;
}
if ( -f $statusFile ) {
   open STATUS, "<$statusFile" or die "could not open $statusFile: $!\n";
   chomp( (%entries) = map { split "\t" } my (@a) = <STATUS> );
   close STATUS;
}
if ( ( exists $entries{$hostname} ) && ( $entries{$hostname} eq $realIP ) ) {
   print "dyndd.net: Already set to the correct IP, [$hostname] = [$realIP]\n";
   syslog('info', '%s', "Already set to the correct IP, [$hostname] = [$realIP]");
} else {
   $entries{$hostname} = $realIP;
   open STATUS, ">$statusFile" or die "could not open $statusFile: $!\n";
   foreach my $key ( keys %entries ) {
      print STATUS "$key\t$entries{$key}\n";
   } # foreach
   my $output = &doUpdateNS( $hostname, $realIP );
   print "$hostname updated to $realIP, results are\n$output\n";
   syslog('info', '%s', "$hostname updated to $realIP" );
}
closelog();

1;