#!/usr/bin/perl -w
# ddns.pl
# pod at tail
$|++;
use strict;
use IO::Socket;
use IO::Interface;
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
my $id = '';
my $pass = '';
my $site = '';
my $fqdn = '';
my $if = 'ppp0';
print "\n";
print "Start dynamic DNS update for $fqdn:\n";
print "\n";
my $s = IO::Socket::INET->new(Proto => 'udp');
my @interfaces = $s->if_list;
print "Check $if status: ";
my $ifup = 0;
for(@interfaces){
$ifup++ if( $_ =~/$if/ );
}
if($ifup == 0){
print "INACTIVE - ABORT\n\n";
exit 0;
}
print "ACTIVE\n";
print "Query host for $if address: ";
my $addr;
if($addr = $s->if_addr($if)) { print "PASS\n"; }
else { print "FAIL:\n$!"; }
print "Post $if IP address to DynDNS service: ";
my $ua = LWP::UserAgent->new();
$ua->credentials($id, $pass);
my $request = POST "$site", [ip => $addr, action => 'update'];
$request->authorization_basic($id, $pass);
my $response = $ua->request($request);
if ($response->is_success) {
print "PASS\n";
} else {
print "FAIL:\n", $response-> message, "\n";
exit 0;
}
print "Check DNS propagation via name lookup: ";
my $packed_address = gethostbyname($fqdn);
unless ($packed_address) {
print "FAIL:\n $!";
exit 0;
}
print "PASS\n";
print "Compare resolved address to local: ";
my $dotted_quad = inet_ntoa($packed_address);
if($dotted_quad eq $addr){
print "PASS\n";
} else {
print "FAIL:\n$!";
exit 0;
}
print " $dotted_quad <= $fqdn\n";
print " $addr <= $if\n";
print "\n";
print "Finished dynamic DNS update for $fqdn:\n\n";
exit 1;
######################################################################
+#####
=head1 NAME
ddns.pl
=head1 DESCRIPTION
Update dynamicDNS service for computer on dial-up connection.
=head1 USAGE
Accepts no arguments.
Create symlink in /etc/ppp/ip-up.d/ so will be
called whenever dial-up connection is (re)establised:
ln -isv /path/to/ddns.pl /etc/ip-up.d/ddns
Works well in conjuntion with /etc/ppp/peers/provider 'persist' and
'holdoff' parameters plus /etc/ppp/ppp_on_boot for nailed-up connecti
+on.
=head1 REQUIREMENTS
perl 5
libwww-perl
IO::Interface
make to install modules
gcc " " "
libc6-dev " "
/etc/nsswitch => hosts: dns files
so name confirm is from DNS not /etc/hosts.
=head1 UPDATE
November 27, 2001 20:20 CST
Compare local interface IP address with DNS lookup address.
Check for interface active before proceeding.
Confirm DNS updated correct IP address with gethostbyname().
Use IO::Socket+Interface to determine local ppp0 IP address
instead of ifconfig with backticks.
Eliminate (unecessary) subroutines.
October 27, 1999
Initial working code.
=head1 CREDITS
jwest for nsswitch, libc6-dev tips
vroom for PerlMonks
=head1 AUTHOR
ybiC
=cut
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|