[SOLVED] qm agent XXX ping - Shell return value in PHP

MisterDeeds

Active Member
Nov 11, 2021
143
33
33
35
Dear all

I would like to check the agent status of a VM using a PHP script. I have created this as follows:

PHP:
$connection = ssh2_connect($row['Ip'], $row['Port']);
ssh2_auth_password($connection, $row['User'], $row['Password']);
$stream = ssh2_exec($connection, "qm agent ".$row["VmID"]." ping");
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$sshReturn = trim(stream_get_contents($stream_out));

print strlen($sshReturn) == 0 ? 'OK' : 'NOK';

Unfortunately, it is not clear to me how to get the return value. If the agent is running nothing is output, if it is not running the message "VM XXX is not running" appears.

Can someone help me?

Thank you and happy new year!
 
Hello,

To check if the agent is running, you can check the length of the output string.

If the agent of the VM running, there is no output, but if the VM is not running will print (VM 100 is not running).

I guess in your case, you have to set `if statement` to check the length of the output string and print the appropriate message.
 
Dear Moayad

Thank you for the answer. I have now figured it out. The message is written in the error stream. Here is my solution:

PHP:
$connection = ssh2_connect($row['Ip'], $row['Port']);
ssh2_auth_password($connection, $row['User'], $row['Password']);
$command = "qm agent ".$row["VmID"]." ping";

$stream = ssh2_exec( $connection, $command );
$error_stream = ssh2_fetch_stream( $stream, SSH2_STREAM_STDERR );
stream_set_blocking( $stream, TRUE );
stream_set_blocking( $error_stream, TRUE );
$output = stream_get_contents( $stream );
$error_output = stream_get_contents( $error_stream );
fclose( $stream );
fclose( $error_stream );

print strlen($error_output) == 0 ? 'OK' : 'FAIL';

Thanks and best regards
 
  • Like
Reactions: Moayad

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!