Guest User

afraid-dyndns Rev2

a guest
Dec 8th, 2011
412
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2.  
  3. #
  4. #   afraid-dyndns - a Dynamic DNS client for the afraid.org free service
  5. #   Copyright (C) 2009 Erick Calder
  6. #   Copyright (C) 2011 platnicat mewr
  7. #
  8. #   This program is free software; you can redistribute it and/or modify
  9. #   it under the terms of the GNU General Public License as published by
  10. #   the Free Software Foundation; either version 3 of the License, or
  11. #   (at your option) any later version.
  12. #
  13. #   This program is distributed in the hope that it will be useful,
  14. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. #   GNU General Public License for more details.
  17. #
  18. #   You should have received a copy of the GNU General Public License
  19. #   along with this program; if not, write to the Free Software
  20. #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21. #
  22.  
  23. $|++;
  24. $\ = $/;
  25.  
  26. use LWP::Simple;
  27. use XML::Simple;
  28. # use Data::Dumper;
  29. use Getopt::Long;
  30.  
  31. Getopt::Long::Configure("no_ignore_case");
  32. $args{"quiet"} = "Suppresses normal output";
  33. $args{"help"} = "Display syntax";
  34. GetOptions(\%ARGV, keys %args);
  35.  
  36. if ($ARGV{help}) {
  37.     echo("Syntax: $0 [--quiet] [--help] [hostname]");
  38.     echo("    quiet: supresses all output");
  39.     echo("    help: displays syntax");
  40.     echo("    hostname: indicates that only the specified name should be refreshed");
  41.     exit;
  42.     }
  43.  
  44. # afraid.org listing of urls
  45.  
  46. $afraid = "http://freedns.afraid.org/api/?action=getdyndns&sha=%s&style=xml";
  47. ($VER = substr $Revision: 2 $, 10) =~ s/\s+$//;
  48. $CACHEDIR = "/var/cache/afraid-dyndns/IP"; # set cache directory
  49. $HASH = "<your_hash>"; # account hash for authentication
  50. ($ME = $0) =~ s|.*/||;
  51. $hostname = shift;
  52.  
  53. echo("Original script by Erick Calder, this fork by platnicat mewr");
  54. echo("-- $0 (Ver: $VER) --");
  55. echo("License: GPL");
  56.  
  57. #
  58. # get external and cached IP addresses
  59. #
  60.  
  61.  
  62. $extip = get("http://automation.whatismyip.com/n09230945.asp"); # Fetch current IP from whatismyip
  63. die "Failed to get IP" unless $extip; # If IP wasn't acquired, go poof
  64. $intip = readfile($CACHEDIR);   # internal address (cached)
  65. chomp $intip;
  66.  
  67. echo("Read $intip as cached IP, $extip is the new one, if applicable");
  68.  
  69. #
  70. # when these differ modify the DNS and cache the address
  71. #
  72.  
  73. if ($extip eq $intip) {   # check if the new IP and the current IP are the same
  74.     echo("No change in address!");
  75. } else {
  76.     echo("Address changed: $intip => $extip");
  77.  
  78.     $xml = get(sprintf($afraid, $HASH));  
  79.     die "Failed fetching update head!" unless $xml;
  80.     $o = XMLin($xml);
  81.     writefile($CACHEDIR, $extip);
  82.     }
  83.  
  84. exit;
  85.  
  86. #
  87. #   Syntax:
  88. #       readfile [file-name = $_]
  89. #       <scalar> = readfile [file-name = $_]
  90. #       <list> = readfile [file-name = $_]
  91. #   Synopsis:
  92. #       returns the contents of a given file.  if called in a void
  93. #       context, this function sets $_; if called in a scalar context
  94. #       the contents are returned as a single string with embedded
  95. #       newlines; and if called in a list context the content comes
  96. #       back as separate lines.
  97. #
  98.  
  99. sub readfile {
  100.     my $f = shift || $_;
  101.     local $_ if defined wantarray();
  102.     -f $f || return;
  103.     open(F, $f) || warn($!) && return;
  104.     wantarray() && (@_ = <F>) || (local $/ = undef, $_ = <F>);
  105.     close(F);
  106.     wantarray() ? @_ : $_;
  107.     }
  108.  
  109. #
  110. #   Syntax:
  111. #       writefile <file-name> [content = $_]
  112. #   Synopsis:
  113. #       writes a string to a file, returns success/failure
  114. #
  115.  
  116. sub writefile($@) {
  117.     my $fn = shift;
  118.     local $_ = shift || $_ || return;
  119.  
  120.     print ">> writefile(): $fn" if $::DEBUG;
  121.  
  122.     mkpath(path2fn($fn));
  123.     open(OUT, "> $fn") || warn(qq|writefile("> $fn"): "$!"|) && return;
  124.     print OUT;
  125.     close(OUT) || return;
  126.     return 1;
  127.     }
  128.  
  129. #
  130. #   could've used system("mkdir -p") but
  131. #   that won't work on Win32 systems
  132. #
  133.  
  134. sub mkpath {
  135.     local $_ = shift;
  136.     my @d = split m(/);
  137.     my $d = "";
  138.     my $mkpath = 0;
  139.  
  140.     for (@d) {
  141.         $d .= "$_/";
  142.         next if -d $d;  # skip if it already exists
  143.         print "- $d" if $::DEBUG > 1;
  144.         mkdir($d) || warn(qq/mkdir("$d"): "$!"/) && return;
  145.         $mkpath = 1;
  146.         }
  147.  
  148.     return 1;
  149.     }
  150.  
  151. #
  152. #   splits a path into directory, filename and extension
  153. #   e.g. ($dir, $fn, $ext) = path2fn($path)
  154. #
  155.  
  156. sub path2fn {
  157.     my $path = shift;
  158.     my ($dir, $fn, $ext);
  159.  
  160.     @x = split(/\//, $path);
  161.     $fn = pop @x;
  162.     $dir = join("/", @x);
  163.     @x = split(/\./, $fn);
  164.     if (@x > 1) {
  165.         $ext = pop @x;
  166.         $fn = join(".", @x);
  167.         }
  168.  
  169.     return ($dir, $fn, $ext);
  170.     }
  171.  
  172. #
  173. #   output functions
  174. #
  175.  
  176. sub debug {
  177.     print shift if $ARGV{debug} && !$ARGV{quiet};
  178.     }
  179.  
  180. sub echo {
  181.     print shift || $_ unless $ARGV{quiet};
  182.     }
  183.  
RAW Paste Data