Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- # -------------------------------------------------------------------------------
- # neobitti_update_ip.pl
- #
- # Version 1.0 - 16.01.2012
- #
- # PERL script to dynamically update the IP of a host via the cPanel-API. This
- # script was written to work with the Finnish hoster Neobitti but it might work
- # with other hosters which use cPanel too.
- #
- # Copyright (C) 2012 Stefan Gofferje - http://stefan.gofferje.net/
- #
- # This program is free software; you can redistribute it and/or
- # modify it under the terms of the GNU General Public License as
- # published by the Free Software Foundation; either version 2 of the
- # License, or (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful, but
- # WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- # General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- # -------------------------------------------------------------------------------
- use strict;
- use LWP::UserAgent;
- use MIME::Base64;
- use XML::Simple;
- use Data::Dumper;
- # --- Command line parameters ------------------------------------------------
- my $param_domain=$ARGV[0];
- my $param_host=$ARGV[1];
- my $param_ip=$ARGV[2];
- # --- cPanel information -----------------------------------------------------
- # Storing passwords in clear text is ugly!
- my $cpanel_domain = "yourdomain.com";
- my $user = "yourcpaneluser";
- my $pass = "yourcpanelpassword";
- my $auth = "Basic " . MIME::Base64::encode( $user . ":" . $pass );
- # --- Deactivate SSL certificate validation ----------------------------------
- # This is ugly but neccessary because Neobitti uses self-signed SSL
- # certificates which will fail validation
- my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
- # --- Find out the linenumber for the A-record we want to change -------------
- sub getlinenumber_a {
- my $domain=$_[0];
- my $hostname=$_[1].".";
- my $xml = new XML::Simple;
- my $request = HTTP::Request->new( GET => "https://$cpanel_domain:2083/xml-api/cpanel?cpanel_xmlapi_module=ZoneEdit&cpanel_xmlapi_func=fetchzone&domain=$domain" );
- $request->header( Authorization => $auth );
- my $response = $ua->request($request);
- my $zone = $xml->XMLin($response->content);
- my $linenumber="";
- if ($zone->{'data'}->{'status'} eq "1") {
- my $count = @{$zone->{'data'}->{'record'}};
- my $oldip="";
- for (my $item=0;$item<=$count;$item++) {
- my $name=$zone->{'data'}->{'record'}[$item]->{'name'};
- my $type=$zone->{'data'}->{'record'}[$item]->{'type'};
- if ( ($name eq $hostname) && ($type eq "A") ) {
- $linenumber=$zone->{'data'}->{'record'}[$item]->{'Line'};
- $oldip=$zone->{'data'}->{'record'}[$item]->{'record'};
- print "Found $hostname in line $linenumber with IP $oldip.\n"; # DEBUG
- # --- If the new ip and the old ip are the same... Finish script -----
- if ( $oldip eq $param_ip ) { print "New and old ip are the same. Finishing script.\n"; exit; };
- }
- }
- } else {
- $linenumber="0";
- print $zone->{'event'}->{'data'}->{'statusmsg;'}
- }
- return($linenumber);
- }
- # --- Change the IP address record for a certain linenumber ------------------
- sub setip {
- my $domain=$_[0];
- my $linenumber=$_[1];
- my $newip=$_[2];
- my $result="";
- my $xml = new XML::Simple;
- 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" );
- $request->header( Authorization => $auth );
- my $response = $ua->request($request);
- my $reply = $xml->XMLin($response->content);
- if ($reply->{'data'}->{'status'} eq "1") {
- $result="1";
- } else {
- $result=$reply->{'data'}->{'statusmsg'};
- }
- return($result);
- }
- # --- Main procedure ---------------------------------------------------------
- print "Trying to find the linenumber for $param_host in $param_domain...\n";
- my $line=getlinenumber_a($param_domain,$param_host);
- if ( ($line ne "0") && ($line ne "") ) {
- print "Trying to update IP...\n";
- my $result=setip ($param_domain,$line,$param_ip);
- if ($result eq "1") {
- print "Update successful!\n";
- } else {
- print "$result\n";
- }
- } else {
- print "Error - check domain and hostname!\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment