Sexifying WHM with XML API

Posted by Nessa | Posted in ,,,, | Posted on June 7, 2008

6

.!.

I don’t know about you other cPanel system admins out there, but I find WHM to be very useful for the more advanced and time-consuming tasks, such as installing SSL certificates. However, the easy stuff like changing an account’s package and resetting passwords is a royal pain in the ass as far as convenience is concerned when you have to log into WHM, list accounts, and make whatever change.

I recently became favorable towards the WHM XML API functionality which will let me do a majority of the everyday account-related tasks from command line without ever opening my browser, which is a lot easier when managing thousands of users across multiple servers. Below are a couple scripts I’ve put together using the XML API from a base script in the cPanel forums:

Change account password

Change account package

Both are run via command line, and the arguments passed to the PHP script as variables. For example, to change an account’s password:

./chacctpass myuser mypass1234

Customizing these scripts to perform different functions is easy via the following steps:

- change if ($argc != 3) to the number of command line arguments you wish to pass to the script plus one. In the above example there are two arguments and since the script name counts, add one and that makes 3.

- in the section where the arguments are assigned to variables (like $cpuser, etc), name your variables. The first one should have an array value of 0, then 1, 2, etc.

- edit the usage example, which will come up if the required number of arguments is not provided…you can add any text you like

- if you’re using a hash (which is more secure than user/pass authentication), go fetch your remote access key from WHM and put it in the $hash value within quotes, format intact. Otherwise, put in your WHM user’s username and password

- change the $server variable to your server’s hostname

- change $apipath to the WHM path for the function you are using. You can find a whole list of them here, and most will give you the path to use in the examples sections. In the API path, insert your variable names where the values are suppose to be. For instance:

$apiPath = “/xml-api/passwd?user=myuser&pass=mypass1234″;

Would be:

$apiPath = “/xml-api/passwd?user=$cpuser&pass=$newpass”;

In the header section, uncomment whichever $header .= “Authorization: line that matches your authentication method (user/pass or hash)

Once you’ve configured your API script, chmod to 700 and run from the command line as show in my example. It’s better to lock down the script by changing its ownership only to the user that will be using it, and not giving read, write, or execute permissions to anyone else.

Note: for these scripts to work you have to have PHP compiled with OpenSSL support, otherwise change the socket variables to http over port 2086.

Be Sociable, Share!

Comments (6)

Account passwords for WHM/cPanel use the system password, so you can easily just change the pass with the “passwd user” command at the BASH prompt. All that does is edit the /etc/passwd file like passwd does, only you’re not sending it out as plaintext over an HTTP socket (security risk, especially if you’re not doing it over SSL!)

The whole idea has some real potential though, aside from that. I’ll have to look into what else it can do and write something cool in Python.

@BinaryPhalanx

The reset password function in WHM and cPanel do more than update /etc/shadow. Some services, such as MySQL, do not use /etc/shadow for authentication. The reset password function also updates the databases for MySQL, FTP and other services.

If you are executing the scripts on the same host where changes need made, you could probably send them to localhost:port. Also, if you are using internal, non-public IP Addresses, your risk is lessened as the requests won’t be routed via the Net.

[...] a single script that accepts variables from any authenticated source.  So a little while ago I posted about the new cPanel XML API and how to integrate that with your own scripts – well now, I’ll use that as an example to [...]

Excellent posting! looking forward to much deployment of cpxmlapi. is totally underutilized. thanks Vanessa…very sexy indeed 8-)

Is that your photo on the top banner?

Post a comment