PHP script for editing DNS records in CPanel

I would like to become self-sufficient and, therefore, do without services such as DNSDynamic and DYNDNS. And I do not like to pay for services that I could do myself.

So, here is the scenario - I have my main site hosted by a hosting company. I also have a home server with music, etc. But the problem is that my provider (BT) does not offer users static IP addresses.

I would like to have a subdomain of my primary domain (which points to my primary host) to my home IP address. This is done with a simple A record, which I made myself.

It comes down to the fact that I want to make a PHP script (executed by a cron job on my home server) to update the A entries in cPanel to the current IP address of my home server.

Here is an example code - of course, a bit that is missing is the API code for communicating with cPanel, and I would really appreciate anyone who could fill in the blanks for me.

<?php
    $current_ip = file_get_contents("http://mydomain.com/getip.php");
    $username = "CPANEL_USERNAME";
    $password = "CPANEL_PASSWORD";
    $domain = "CPANEL_DOMAIN";

    $request = file_get_contents("http://someapipage?username=".$username."&pw=".$password."&domain=".$domain."&ip=".$current_ip);

?>

The code in http://mydomain.com/getip.phpis something like simple

<?php echo $_SERVER["REMOTE_ADDR"]; ?>

I already have an understanding of how to configure the cron job, since Ubuntu is running on my home server, and I read the tutorials that call mine cron.phpin my localhost directory with wget.

I tried this link , but I could not understand what it was doing. Thanks in advance.

+6
source share
3 answers

I just wrote this library for cPanel jSON-APIs based on their documentation and jordih.net's links to this documentation. This is not well documented, but the bottom line is:

Create a zone_records object by calling

$zones = new zone_records("cpaneluser", "pass", "website_to_login", "domain_of_records")

, - 127.0.0.1, , .

$zones->DNSrecords. DNS A CNAME ( DNSrecord). ( TXT) , () .

DNS (, target, ttl, name, type), , private, $record->ttl, "" __get. "" __set ttl target ( , API, , ).

$zones->addrecord($type, $target, $name, $ttl)

,

$zones->deleterecord($line)

, $line - $record->line.

ZoneEdit,

$zones->doquery("function_from_API", array("parameters=>"here"), array("headers"=>"here"))

cPanel ( addrecord deleterecord). , try {...} catch (Exception $e) {...}, , / - ( , ).

- https://github.com/ShadowLNC/cpanel_dns (classdns.php - , dns_update.php ).

+12

. . script :

#!/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 = "example.com";
my $user = "username";
my $pass = "password";
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="";
  print $response->content . "\n";
  print $zone . "\n";
  print $zone->{'data'}->{'status'} . "\n";
  print $zone->{'data'}->{'record'} . "\n";
  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'};
        print $name;
        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
        }
    }
  } 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";
}

"username" "password" .

script , IP :

./neobitti_update_ip.pl <domain> <subdomain> <ip>

:

./neobitti_update_ip.pl example.com subdomain.example.com 93.184.216.34

:


Perl- script , :

http://stefan.gofferje.net/it-stuff/scripts/50-dynamic-dns-update-via-cpanel-api

PHP, , Perl PHP ( ). script .

+2

bash linux, , , -, IP-. ddclient , , , () , cPanel ( serverprofis.de, ).

The bash script is located at github , and I just edited the credentials, etc. that were found on cPanel in the code, and then added a cron job to execute it every few minutes. The script, like ddclient, checks if the IP has changed, and stops if no changes are detected compared to the last update of the provider. If the IP address has changed, it changes the record with the provider, indicating the new IP address.

So far, it works well.

0
source

All Articles