Skip to main content

How to Write Your Own Utilities Using Plesk API-RPC and CLI

api

Every now and then the Plesk development team gets requests to expand the functionality of existing command line utilities or to add new ones. We try to take such requests into account, but, seeing as major Plesk releases only happen roughly once a year, many months may pass before a customer’s vision becomes a reality. However, there is another way to get the desired functionality. If you find yourself in a situation where no existing utility does exactly what you want to, you can create your own using Plesk API-RPC and expanding upon the functionality of existing CLI utilities.

Custom utilities are created by using a command line API client to send an API call to Plesk (local or remote) and receive the response, or by incorporating an existing CLI utility into a shell script. Our GitHub repository contains usable examples of command line API clients in a dozen different programming languages. You only need to modify them and, potentially, process the output (which will come in the form of an .xml response packet). This way you can create a utility that is able to do anything you can do via API (by the way, to learn what you can do via API, take a look at our API-RPC guide).

I will illustrate the concept with two examples. The first is written in bash, and it tells you on which Plesk server a particular domain is hosted. The second is written in PHP, and it displays resource usage data for all subscriptions on the server.

Where is example.com?

Imagine that you are a Plesk administrator, running a number of Plesk servers. You need to make some changes to the configuration of the example.com domain, but you cannot quite remember on which server the domain is hosted. Yes, you can log in to your billing solution (if you have one), or check every server in turn, but… doesn’t it sound like too much effort? There is a better solution. You can run the following custom script making use of the Plesk domain CLI utility instead:

First, create a file named whereisit.sh and paste the following code into the file:

Put the hostnames of all Plesk servers to be searched after SERVERS. Separate individual hostnames with the “space” symbol. Make the file executable:

and run it:

The script will search all the servers you specify and provide output of the following form:

Getting detailed resource usage information

If you need to retrieve information about resource usage on the server for every subscription and store it in a file, the following script will help you. To use it, first create a file named PleskApiClient.php and paste the code found here into it, then create a file named getstats.php and paste the following code inside:

Place both files in the same directory and run the script like this:

The script will output the resource usage information in the semicolon-delimited CSV format, so you can, for example, import it in Excel. Here is a sample output:

These two examples are merely the tip of the iceberg – you can create all kinds of custom utilities with this method. Your imagination (well, and your programming skills, too) is the limit here. Remember to refer to the API-RPC guide, and we also recommend you to use a browser extension for working with API to make your life easier. If you are using Google Chrome, Postman is a fine choice, but there are others as well – shop around and see what fits you best. You can also use the XML-RPC API Explorer Plesk extension – it is a handy tool to help you create and validate your API requests.

3 thoughts

  1. Oliver Lippert -

    Hey there,
    I’am searching a way to find Domains & subdomains and list them WITH their configured PHP-Version.

    Therefor I use packet->domain->get with no filter and dataset->gen_info to get all Domains (have to figure out how to get subdomains) and then for each Domain I tryed packet>Domain>get-physical-hosting-descriptor with filter->domain-name->#DOMAINNAME#…

    But I can’t find the php-version-number in the Reply!?

    May you assist?

    Would be really great 🙂

    Reply

    Alexey Stepanov -

    Thanks for the comment, Oliver.
    Unfortunately, at the present moment there is no way to obtain the PHP version for a domain via API. In Plesk 12.5 we added the method, which will enable you to get the information you seek in a slightly roundabout way. You can first retrieve the PHP handler ID using the method, then retrieve the properties of the handler, including the PHP version.

    Request sample:
    <packet>
    <php-handler>
    <get>
    <filter>
    <id>fastcgi</id>
    </filter>
    </get>
    </php-handler>
    </packet>

    Response sample:
    <packet version=”1.6.7.0″>
    <php-handler>
    <get>
    <result>
    <status>ok</status>
    <id>fastcgi</id>
    <display-name>5.3.3</display-name>
    <full-version>5.3.3</full-version>
    <version>5.3</version>
    <type>fastcgi</type>
    <path>/usr/bin/php-cgi</path>
    <clipath>/usr/bin/php</clipath>
    <phpini>/etc/php.ini</phpini>
    <custom>false</custom>
    <handler-status>enabled</handler-status>
    </result>
    </get>
    </php-handler>
    </packet>

    Reply

  2. Fred -

    Is there any way to set the version? (I have multiple php versions installed)

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

0 Shares
Tweet
Share
Share
Buffer
Reddit
+1