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...
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 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.
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