...
...
The REST API allows users to query the iQSonar results directly using the web client protocol. The results are returned in JSON format.
This worked example uses PHP to produce results in HTML to display in a web browser, containing details of devices directly from the scan results. We look for the host name, the total installed RAM and the CPU type. The device name will be listed in the "device" results. For the CPU Type and the installed RAM we need to then go to the details page for the device.
This article is one in a series showing how to use the REST API to produce sample output. Other examples producing equivalent results in CSV format have been written for Python, PERL and for PowerShell
...
- The script is written in PHP 7. It should work with any web server that supports PHP 7 (e.g. Apache, IIS, Nginx)
- The script relies on two optional modules php7-curl for web requests and php7-json
- You need to know the url for your iQSonar instance
- You need to know the basic authentication credentials for your iQSonar instance
Step One - build the CURL connection to the Rest API host
...
Code Block | ||||
---|---|---|---|---|
| ||||
// update these for your location $rhost = "vm-mike-sql17"; $aname = "admin"; $psswd = "password"; /* * Use curl to fetch the results from the REST API */ $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_USERPWD, $aname . ":" . $psswd); curl_setopt($ch, CURLOPT_URL, "http://" .$rhost ."/API/v1/devices"); $response = curl_exec($ch); // We want to parse the headers to find X-fetch-count $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $headers = substr($response, 0, $header_size); // explode multiline response headers into an array of lines $header = explode("\n",$headers); foreach ($header as $line) { // find the two headers we're specifically interested in list($key,$value) = explode(": ",$line); if (strcasecmp($key,"X-fetch-count")==0) { $max = $value; } // total no of devices in database if (strcasecmp($key,"X-fetch-current-size")==0) { $count = $value; }// count of devices returned in THIS query - will stop at 200 if there are more than 200 results } $body = substr($response, $header_size); $jsondata = json_decode($body, true); // recursive associative array please |
By default the REST API "/api/v1/devices" page will return 200 devices at a time. We can increase or decrease this using the "fetch_size" parameter in the URL. The X-fetch-count header contains the total number of devices in the dataset. The X-fetch-current-size header shows how many were returned in this batch of results.
...