Guest User

Dynamic DNS update via cPanel API

a guest
Apr 7th, 2014
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.53 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # -------------------------------------------------------------------------------
  3. # neobitti_update_ip.pl
  4. #
  5. # Version 1.0 - 16.01.2012
  6. #
  7. # PERL script to dynamically update the IP of a host via the cPanel-API. This
  8. # script was written to work with the Finnish hoster Neobitti but it might work
  9. # with other hosters which use cPanel too.
  10. #
  11. # Copyright (C) 2012 Stefan Gofferje - http://stefan.gofferje.net/
  12. #
  13. # This program is free software; you can redistribute it and/or
  14. # modify it under the terms of the GNU General Public License as
  15. # published by the Free Software Foundation; either version 2 of the
  16. # License, or (at your option) any later version.
  17. #
  18. # This program is distributed in the hope that it will be useful, but
  19. # WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  21. # General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU General Public License
  24. # along with this program; if not, write to the Free Software
  25. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  26. # -------------------------------------------------------------------------------
  27. use strict;
  28. use LWP::UserAgent;
  29. use MIME::Base64;
  30. use XML::Simple;
  31. use Data::Dumper;
  32. # --- Command line parameters ------------------------------------------------
  33. my $param_domain=$ARGV[0];
  34. my $param_host=$ARGV[1];
  35. my $param_ip=$ARGV[2];
  36. # --- cPanel information -----------------------------------------------------
  37. # Storing passwords in clear text is ugly!
  38. my $cpanel_domain = "yourdomain.com";
  39. my $user = "yourcpaneluser";
  40. my $pass = "yourcpanelpassword";
  41. my $auth = "Basic " . MIME::Base64::encode( $user . ":" . $pass );
  42. # --- Deactivate SSL certificate validation ----------------------------------
  43. # This is ugly but neccessary because Neobitti uses self-signed SSL
  44. # certificates which will fail validation
  45. my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
  46. # --- Find out the linenumber for the A-record we want to change -------------
  47. sub getlinenumber_a {
  48.   my $domain=$_[0];
  49.   my $hostname=$_[1].".";
  50.   my $xml = new XML::Simple;
  51.   my $request = HTTP::Request->new( GET => "https://$cpanel_domain:2083/xml-api/cpanel?cpanel_xmlapi_module=ZoneEdit&cpanel_xmlapi_func=fetchzone&domain=$domain" );
  52.   $request->header( Authorization => $auth );
  53.   my $response = $ua->request($request);
  54.   my $zone = $xml->XMLin($response->content);
  55.   my $linenumber="";
  56.   if ($zone->{'data'}->{'status'} eq "1") {
  57.     my $count = @{$zone->{'data'}->{'record'}};
  58.     my $oldip="";
  59.     for (my $item=0;$item<=$count;$item++) {
  60.         my $name=$zone->{'data'}->{'record'}[$item]->{'name'};
  61.         my $type=$zone->{'data'}->{'record'}[$item]->{'type'};
  62.         if ( ($name eq $hostname) && ($type eq "A") ) {
  63.           $linenumber=$zone->{'data'}->{'record'}[$item]->{'Line'};
  64.           $oldip=$zone->{'data'}->{'record'}[$item]->{'record'};
  65.           print "Found $hostname in line $linenumber with IP $oldip.\n"; # DEBUG
  66.           # --- If the new ip and the old ip are the same... Finish script -----
  67.       if ( $oldip eq $param_ip ) { print "New and old ip are the same. Finishing script.\n"; exit; };
  68.         }
  69.     }
  70.   } else {
  71.     $linenumber="0";
  72.     print $zone->{'event'}->{'data'}->{'statusmsg;'}
  73.   }
  74.   return($linenumber);
  75. }
  76. # --- Change the IP address record for a certain linenumber ------------------
  77. sub setip {
  78.   my $domain=$_[0];
  79.   my $linenumber=$_[1];
  80.   my $newip=$_[2];
  81.   my $result="";
  82.   my $xml = new XML::Simple;
  83.   my $request = HTTP::Request->new( GET => "https://$cpanel_domain:2083/xml-api/cpanel?cpanel_xmlapi_module=ZoneEdit&cpanel_xmlapi_func=edit_zone_record&domain=$domain&line=$linenumber&address=$newip" );
  84.   $request->header( Authorization => $auth );
  85.   my $response = $ua->request($request);
  86.  
  87.   my $reply = $xml->XMLin($response->content);
  88.   if ($reply->{'data'}->{'status'} eq "1") {
  89.     $result="1";
  90.   } else {
  91.     $result=$reply->{'data'}->{'statusmsg'};
  92.   }
  93.   return($result);
  94. }
  95. # --- Main procedure ---------------------------------------------------------
  96. print "Trying to find the linenumber for $param_host in $param_domain...\n";
  97. my $line=getlinenumber_a($param_domain,$param_host);
  98. if ( ($line ne "0") && ($line ne "") ) {
  99.   print "Trying to update IP...\n";
  100.   my $result=setip ($param_domain,$line,$param_ip);
  101.   if ($result eq "1") {
  102.     print "Update successful!\n";
  103.   } else {
  104.     print "$result\n";
  105.   }
  106. } else {
  107.   print "Error - check domain and hostname!\n";
  108. }
Advertisement
Add Comment
Please, Sign In to add comment