#!/usr/bin/perl use strict; if (($ARGV[0] ne "v4") and ($ARGV[0] ne "v6")) { print "Usage: $0 v4|v6\n"; exit 1; }; # Check for IPV6 requ. my $ipver = "v4"; if ($ARGV[0] eq "v6") { require Net::INET6Glue::INET_is_INET6; $ipver = "v6"; }; my $previp = ''; # DNS record types: my %drts = ( 'v4' => 'A', 'v6' => 'AAAA', ); # Checkip url my $checkurl = 'http://www.example.com.gr/myip'; # Updateip url my $updateurl = 'http://www.example.com/ddnsset.pl?'; # FQDN to set: my $fqdn = 'myhost'; # History filenames: my %iphist = ( 'v4' => '/tmp/dynipsethistv4.txt', 'v6' => '/tmp/dynipsethistv6.txt', ); # Load history: if (-e $iphist{$ipver}) { open (HI, "<", $iphist{$ipver}) or die "Failed to open " . $iphist{$ipver} . " for reading: $!.\n"; $previp = ; close HI; }; # Create a user agent object use LWP::UserAgent; my $ua = LWP::UserAgent->new; $ua->agent("dynipset/0.1 "); # Create a request my $req = HTTP::Request->new(GET => $checkurl); # Pass request to the user agent and get a response back my $res = $ua->request($req); # Check the outcome of the response if ($res->is_success) { my $newip = $res->content; #print "DEBUG: My new IP is $newip ($ipver) \n"; if ($previp eq $newip) { # Do nothing. #print "DEBUG: IP not changed.\n"; exit 0; } else { # Set ip: my $dbrec = "$fqdn|" . $drts{$ipver} . "|$newip"; my $rc = &setDNS($dbrec); if ($rc) { print "SET: $fqdn " . $drts{$ipver} . " $newip\n"; } else { print "ERROR: setDNS failed ($fqdn|$newip)!\n"; exit 1; }; # Save new ip to history: open (HI, ">", $iphist{$ipver}) or die "Failed to open " . $iphist{$ipver} . " for writing: $!.\n"; print HI $newip; close HI; exit 0; }; } else { print "ERROR: checkip request failed: " . $res->status_line, "\n"; exit 1; }; exit 0; sub setDNS { my ($dbrec, $rest) = @_; use Crypt::CBC; use MIME::Base64 qw(encode_base64url decode_base64url); my $cipher = Crypt::CBC->new( -key => 'a pass phrase', -cipher => 'Blowfish', -keysize => 56 ); my $ciphertext = $cipher->encrypt($dbrec); my $encoded = encode_base64url($ciphertext); #print "DEBUG: $encoded\n"; my $url = "$updateurl$encoded"; my $ua = LWP::UserAgent->new; $ua->agent("dynipset/0.1 "); # Create a request my $req = HTTP::Request->new(GET => $url); # Pass request to the user agent and get a response back my $res = $ua->request($req); if ($res->is_success) { return 1; } else { print "ERROR: updateip request failed: " . $res->status_line, "\n"; return 0; }; };