phpvariablesif-statementshell-execopenvz

PhP shell_exec output convert to variable, and variable to image


In the days I started developing a VPS contol panel that communicates with the OpenVZ kernel using PhP commands.

I would like to display the status of the VPS, so it works so far.

<?php
$VMstatus = shell_exec("sudo vzctl status 17 | cut -d ' ' -f 5");
echo $VMstatus;
?>

It prints "running".

However, if the term "running" word is in the output, I would like to display an image, but the following code does not display any images.

<?php
$VMstatus = shell_exec("sudo vzctl status 17 | cut -d ' ' -f 5");
echo $VMstatus;

if ($VMstatus == 'running') {
  echo "<img src='/assets/img/vps-running.png'>";
} else {
  echo "<img src='/assets/img/vps-down.png'>";
}
?>

What might be the problem?

Thank you in advance for your help!
Have a nice day!


Solution

  • Your shell outputs whitespaces, you can remove those with the trim method. https://www.php.net/manual/en/function.trim.php

    <?php
    $VMstatus = trim(shell_exec("sudo vzctl status 17 | cut -d ' ' -f 5"));
    echo $VMstatus;
    
    if ($VMstatus == 'running') {
      echo "<img src='/assets/img/vps-running.png'>";
    } else {
      echo "<img src='/assets/img/vps-down.png'>";
    }
    ?>