1 |
#!/usr/bin/perl -w |
2 |
|
3 |
# Script to dynamically update DNS entry for hostname of this box |
4 |
|
5 |
use strict; |
6 |
use Net::DNS; |
7 |
use Sys::Syslog; |
8 |
use Sys::Hostname; |
9 |
use File::Basename; |
10 |
|
11 |
my $domain = "dyn.andrew.net.au"; |
12 |
my $key_name = "updatekey"; |
13 |
my $key = "RttHZIREBzkqIUi+jJw2mg=="; |
14 |
|
15 |
my $RC = 0; |
16 |
my $hostname = hostname; |
17 |
my $fqdn = "${hostname}.${domain}"; |
18 |
|
19 |
my $res = Net::DNS::Resolver->new; |
20 |
|
21 |
openlog(basename($0), "pid", "user"); |
22 |
|
23 |
syslog("info", "interface = $ENV{'PPP_IFACE'}") if ($ENV{'PPP_IFACE'}); |
24 |
syslog("info", "tty = $ENV{'PPP_TTY'}") if ($ENV{'PPP_TTY'}); |
25 |
syslog("info", "speed = $ENV{'PPP_SPEED'}") if ($ENV{'PPP_SPEED'}); |
26 |
syslog("info", "local = $ENV{'PPP_LOCAL'}") if ($ENV{'PPP_LOCAL'}); |
27 |
syslog("info", "remote = $ENV{'PPP_REMOTE'}") if ($ENV{'PPP_REMOTE'}); |
28 |
syslog("info", "ipparam = $ENV{'PPP_IPPARAM'}") if ($ENV{'PPP_IPPARAM'}); |
29 |
|
30 |
if ($ENV{'PPP_IFACE'} ne "ppp0") { |
31 |
syslog("info", "Assuming this is not the first PPP connection, exiting..."); |
32 |
exit 0; |
33 |
} |
34 |
|
35 |
if ($ENV{'PPP_LOCAL'}) { |
36 |
my $query = $res->search($domain, "SOA"); |
37 |
if ($query) { |
38 |
my $nameserver = ($query->answer)[0]; |
39 |
$nameserver = $nameserver->mname; |
40 |
|
41 |
my $update = new Net::DNS::Update($domain); |
42 |
$update->push("pre", yxrrset("${fqdn} A")); |
43 |
$update->push("update", rr_del("${fqdn} A")); |
44 |
$update->sign_tsig($key_name, $key); |
45 |
|
46 |
$res->nameservers($nameserver); |
47 |
my $reply = $res->send($update); |
48 |
|
49 |
if (defined $reply) { |
50 |
if ($reply->header->rcode eq "NOERROR") { |
51 |
syslog("info", "A record deletion succeeded"); |
52 |
} else { |
53 |
syslog("info", "A record deletion failed: " . $reply->header->rcode); |
54 |
} |
55 |
} else { |
56 |
syslog("info", "A record deletion failed: " . $res->errorstring); |
57 |
} |
58 |
|
59 |
$update = new Net::DNS::Update($domain); |
60 |
$update->push("pre", nxrrset("${fqdn} A")); |
61 |
$update->push("update", rr_add("${fqdn}. 300 A $ENV{'PPP_LOCAL'}")); |
62 |
$update->sign_tsig($key_name, $key); |
63 |
|
64 |
$reply = $res->send($update); |
65 |
|
66 |
if (defined $reply) { |
67 |
if ($reply->header->rcode eq "NOERROR") { |
68 |
syslog("info", "A record addition for $ENV{'PPP_LOCAL'} succeeded"); |
69 |
} else { |
70 |
syslog("info", "A record addition for $ENV{'PPP_LOCAL'} failed: " . $reply->header->rcode); |
71 |
} |
72 |
} else { |
73 |
print "Addition failed: ", $res->errorstring, "\n"; |
74 |
syslog("info", "A record addition for $ENV{'PPP_LOCAL'} failed: " . $res->errorstring); |
75 |
} |
76 |
|
77 |
} else { |
78 |
syslog("info", "Unable to determine primary nameserver for $domain"); |
79 |
$RC = 2; |
80 |
} |
81 |
} else { |
82 |
print "PPP_LOCAL environment variable not set\n"; |
83 |
syslog("info", "PPP_LOCAL environment variable not set"); |
84 |
$RC = 1; |
85 |
} |
86 |
|
87 |
closelog(); |
88 |
|
89 |
exit($RC); |