75 |
rodolico |
1 |
#! /usr/bin/perl -w
|
|
|
2 |
|
|
|
3 |
use Sys::Syslog;
|
|
|
4 |
|
|
|
5 |
my $statusFile = '/opt/bin/updatedns.status';
|
|
|
6 |
my $template = '/opt/bin/updatedns.template';
|
|
|
7 |
my $nsupdate = '/usr/bin/nsupdate';
|
|
|
8 |
my $keyfile = '/opt/bin/keys/Kdyndd.net.+157+35226.key';
|
|
|
9 |
|
|
|
10 |
my $entries;
|
|
|
11 |
|
|
|
12 |
sub doUpdateNS {
|
|
|
13 |
my ($hostname, $ip) = @_;
|
|
|
14 |
my $nsupdateFileName = "/tmp/$hostname.nsupdate";
|
|
|
15 |
open TEMPLATE, "<$template" or die "could not open template $template for read: $!\n";
|
|
|
16 |
my $template = join( '', <TEMPLATE> );
|
|
|
17 |
close TEMPLATE;
|
|
|
18 |
$template =~ s/\{hostname\}/$hostname/gi;
|
|
|
19 |
$template =~ s/\{ip\}/$ip/gi;
|
|
|
20 |
open OUTPUT, ">$nsupdateFileName" or die "Could not create $nsupdateFileName: $!\n";
|
|
|
21 |
print OUTPUT $template;
|
|
|
22 |
close OUTPUT;
|
|
|
23 |
return `$nsupdate -k $keyfile $nsupdateFileName`;
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
my $hostname = shift;
|
|
|
29 |
my $realIP = shift;
|
|
|
30 |
|
|
|
31 |
print "$hostname,$realIP\n";
|
|
|
32 |
|
|
|
33 |
$realIP =~ m/^([\d.]+)/;
|
|
|
34 |
$realIP = $1;
|
|
|
35 |
|
|
|
36 |
openlog('updatedns', 'cons,pid', 'user');
|
|
|
37 |
|
|
|
38 |
die "You must send hostname with command\n" unless $hostname;
|
|
|
39 |
|
|
|
40 |
if ( $hostname !~ m/[a-z0-9-]\.dyndd\.net/ ) {
|
|
|
41 |
syslog( 'warning', "Attempt to set incoming server name to invalid host [$hostname]");
|
|
|
42 |
exit;
|
|
|
43 |
}
|
|
|
44 |
if ( -f $statusFile ) {
|
|
|
45 |
open STATUS, "<$statusFile" or die "could not open $statusFile: $!\n";
|
|
|
46 |
chomp( (%entries) = map { split "\t" } my (@a) = <STATUS> );
|
|
|
47 |
close STATUS;
|
|
|
48 |
}
|
|
|
49 |
if ( ( exists $entries{$hostname} ) && ( $entries{$hostname} eq $realIP ) ) {
|
|
|
50 |
print "dyndd.net: Already set to the correct IP, [$hostname] = [$realIP]\n";
|
|
|
51 |
syslog('info', '%s', "Already set to the correct IP, [$hostname] = [$realIP]");
|
|
|
52 |
} else {
|
|
|
53 |
$entries{$hostname} = $realIP;
|
|
|
54 |
open STATUS, ">$statusFile" or die "could not open $statusFile: $!\n";
|
|
|
55 |
foreach my $key ( keys %entries ) {
|
|
|
56 |
print STATUS "$key\t$entries{$key}\n";
|
|
|
57 |
} # foreach
|
|
|
58 |
my $output = &doUpdateNS( $hostname, $realIP );
|
|
|
59 |
print "$hostname updated to $realIP, results are\n$output\n";
|
|
|
60 |
syslog('info', '%s', "$hostname updated to $realIP" );
|
|
|
61 |
}
|
|
|
62 |
closelog();
|
|
|
63 |
|
|
|
64 |
1;
|