Main menu

Power of Perl – Controlling and expanding Cpanel/WHM through the Cpanel/WHM API

When running a shared hosting environment, it is impossible to stay competitive without the use of some form of control panel. And in the world of linux shared web hosting, no control panel is as widely used as cPanel Inc‘s cPanel/WHM combo. The Cpanel team has put a lot of time and effort into the remote administration of WHM, and through that Cpanel. With a little ingenuity and not much work, really, this API can be extended to include any functions you can imagine, up to and including the system administration of the machine itself. So lets look at the basics thereof.
For this example I am going to use perl, for several very good reasons. The first is that 96% (roughly) of cPanel/WHM is programmed in perl and sits in uncompiled files that we can use for reference. Secondly, perl is inherently a system administration language and therefore gives us much more native system functionality, not to mention its powerful regular expression engine. And, lastly, I choose perl because the heart of this entire process is already done for us by cPanel themselves and available for download as a perl package here.

If you look through the package, you will see that it already has quite a bit of functionality built in. In fact, it really has most everything you would need to automate the web hosting process. However that’s not enough for us, we want more!

The first thing we are going to want to edit is the new subroutine in the Accounting package. You can see it is stubbed out already:

This function is going to need to establish 4 things that we will use for the API calls. First, a host name. Simple enough. Second, a user. Generally, this is root, though it can be just about anyone with WHM access. Third, an accesshash. This can be found in the user we specified’s home directory in a text file called .accesshash. If one doesn’t exist, login to WHM through the web interface (https://yoursever:2087/ and goto the remote access hash link and it will generate and create said text document. Finally, we need to know whether or not to use SSL to communicate. There are very few times when you want that to be no.

This is an overly simplified example of a possible solution. In my production code, I use a Class::DBI based class to pull the info I need from a database and pass it into here where this subroutine extracts the information it needs. Pretty easy, isn’t it?

So here we take the first two arguments passed in on ARGV, and assign them to the host and a function for us to run on it. We get the access hash from root’s home directory then we create an Accounting object. I use eval to run the function that was passed in on Accounting object and pass it in what is left in ARGV, if anything. We do some error checking and display them if they exist, then I used Data::Dump to result to the screen. Simple, but it gives us what we need to see the API at work.

So awesome! Now what if we want to expand on this to do something cPanel doesn’t natively do? Suppose we wanted more information than what the loadavg command just gave us? Well, we need to goto the cPanel server itself. We navigate to /usr/local/cpanel/whostmgr/docroot and create a directory in there for our functions. We could create files directly in the docroot, but we don’t want our stuff interfering with cPanel’s, nor vice versa. I called mine hsw. In there, let’s create a file called load.cgi and put this inside:

So we run the subroutine whmreq with the path to our script as an argument. We are given back an array of lines that were returned. We simply parse that out and assign the information to appropriate indexes in a hash table. We then pass the reference of the hash back. By running our stub-app, we get the following:

Well, that gives you the basics you need to expand the capability of cPanel/WHM’s API to include your own functionality and subroutines. Now you can get your cpanel/WHM boxes, even the DNS Only versions, to do anything that perl can do… remotely and programmatically.