BuycPanel Blog

BuycPanel Blog

Latest news and updates

7/29/2015 – How To Use a WHP API Browser From a Perl Script

Posted by Jamison on 29 07 2015.

Perl (Practical Extraction and Report Language) is a stable programming language used generally to perform a wide range of tasks such as web development, GUI development, network and graphical programming, database access, and system administration.  Any browser can be derived from a Perl script because its syntax is almost the same as C language, which is known as the most basic programming language.  It is made up of the features of the many other various programming languages such as Python, COBOL, BASIC-PLUS, awk, sed, Lisp, Ada, and bash.

 

Perl runs on a broad range of platforms including the following:

 

	Win 9x/NT/2000
	Mac (PPC, 68K)
	Unix (Linux, Solaris, HP/UX, SunOS, IRIX, FreeBSD, AIX, etc.)
	Alpha (7.2 and later)
	Symbian
	WinCE
	OpenVMS

 

The fact that there are many things that one can do with Perl makes it the system administrator’s easy best friend.  It is easy to learn especially if you already have a background with basic programming.  It has a…

Perl (Practical Extraction and Report Language) is a stable programming language used generally to perform a wide range of tasks such as web development, GUI development, network and graphical programming, database access, and system administration.  Any browser can be derived from a Perl script because its syntax is almost the same as C language, which is known as the most basic programming language.  It is made up of the features of the many other various programming languages such as Python, COBOL, BASIC-PLUS, awk, sed, Lisp, Ada, and bash.

 

Perl runs on a broad range of platforms including the following:

 

  • Win 9x/NT/2000
  • Mac (PPC, 68K)
  • Unix (Linux, Solaris, HP/UX, SunOS, IRIX, FreeBSD, AIX, etc.)
  • Alpha (7.2 and later)
  • Symbian
  • WinCE
  • OpenVMS

 

The fact that there are many things that one can do with Perl makes it the system administrator’s easy best friend.  It is easy to learn especially if you already have a background with basic programming.  It has a great CGI language and has mod_perl as its embedded Perl interpreter for Apache.  Furthermore, it is an open source software which makes bugs easily spotted and fixed quickly.

 

You may wish to do a WHP API call function using Perl script.  To perform this, follow the steps below as listed by cPanel documentation:

 

STEP 1: Log in to your WHM.

 

STEP 2: Perform an access hash authentication to authenticate the server as the root user.  You can view or retrieve an access hash by visiting the Remote Access Key interface under the “Clusters” section.

 

STEP 3: The created contents will go to the file /root/.accesshash.  You will need this in running the Perl script.  Take note the Perl script will require the LWP::Protocol:https module.  Make sure to run the /scripts/perlinstaller LWP::Protocol::https command to install the module.

 

Meanwhile, the instructions below will assume you are logged in to WHM as root:

 

You can perform a basic API call by creating a DNS zone.  Your URL should look like the following:

 

https://127.0.0.1:2087/cpsess##########/json-api/adddns?api.version=1&domain=sampledomain.com&ip=127.0.0.1

 

Enter the following command on your browser:

 

{ metadata: { result: 1, version:1, reason: ‘Added example.com ok belonging to user root’, command: ‘adddns’ } }

Below is an example of running a WHM API call using a Perl script :

 

!/usr/bin/perl

use strict;

use LWP: : UserAgent;

use LWP: :Protocol :: https’

use MIME: : Base64;

 

my $hash = “accesshashhere”;  (this assigns the access has to the $hash variable as a value)

 

$hash =~ s/\n/ /g;

 

my $auth = “WHM root:” . $hash;

$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;

 

my $ua = LWP: : UserAgent->new;

my $request =  HTTP: : Request->new(GET => https://127.0.0.1:2087/json-api/listaccts?api.version=1):

$request->header( Authorization => $auth );  (uses the $auth value as the call’s authentication information)

my $response = $ua->request($request);  (this uses the LWP :: UserAgent module to run the function)

print $response->content;  (prints the function’s output)

 

If you are having problems with the WHM API calls, submit a support ticket and we will be happy to assist you.