Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

enter image description here

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.

share|improve this question
    
What is the code you are trying to use> –  hexafraction Nov 7 '12 at 11:32
    
function vmstatus() { exec("/usr/bin/virsh list --all | tr -s ' '| cut -f3 -d' ' | sed '1,2d' ",$test); exec("/usr/bin/virsh list --all | tr -s ' '| cut -f4 -d' ' | sed '1,2d' ",$testa); foreach($testa as $status) { foreach($test as $virshlist) { echo "<tr><td>".$virshlist."</td><td>".$status."</td></tr>\n"; } } } –  Poorni Ma Nov 7 '12 at 11:35

2 Answers 2

From http://php.net/manual/en/function.exec.php:

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.

share|improve this answer
    
Thanks for info but i already tried those things.echo "VM List: "; $vm = shell_exec('virsh list'); echo "<pre>$vm</pre>"; but am not getting any output. –  Poorni Ma Nov 7 '12 at 11:39
    
Is it need any connect code as like connecting with sql for virsh to be connected with php code? –  Poorni Ma Nov 7 '12 at 11:42
    
@PoorniMa No, but you can try to use popen( as I will show in my answer. –  hexafraction Nov 7 '12 at 12:08
    
can you give complete code –  Poorni Ma Nov 7 '12 at 12:13
    
@PoorniMa See edits below popen( heading in my answer. –  hexafraction Nov 7 '12 at 12:15

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.