Get Information from Virtual Machine Manager Using Php

Get Information from Virtual Machine Manager Using Php

Is it possible to retrieve information from virtual machine manager using a php code? i tried through exec commands but couldn't get anything in browser as result. Please any help to complete this task.

Am using Ubuntu server 11.04 and kvm as hyper-visor. I have installed virtual machine manager.

Please don't suggest any tool to install.

2

2 Answers

From :

You'll need to use an array to get output.

So, use the following code:

$result=[]; // Create an array
exec('your command line', &$result);  // Remember the & before the result as it ***IS A REFERENCE***
//DO whatever with your result

The problem is that you didn't pass your result array as a reference.

Or, use shell_exec, where your return value is everything returned:

$return=shell_exec($command_line);

You can also use backticks:

$result=`cmdline`

Or, use popen(:

Create a new pointer with:

$handle = popen("/bin/ls", "r");

and then read it:

$read = fread($handle);

and close with:

pclose($handle);

It will block execution until the output stops.

8

PHP functions like exec() or shell_exec() won't help because libvirt executes in root mode, while PHP executes as www-data user. We can give www-data root privileges, but there may be some security issues in that case. So best way is to use libvirt-php API. For example, to list all the VM in php:

<?php
     $conn = libvirt_connect('null', false);
     $doms = libvirt_list_domains($conn);
     print_r($doms);
?>

References:

This will execute smoothly if libvirt is listening on a TCP port. To make libvirt listening on a tcp port, this question will help: I can't use libvirt with listen TCP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.