SHARE
TWEET

afraid-dyndns fork by platnicat

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