0

OS: Ubuntu 16.04

I tried to use variable inside ssh2_exec but I get this message:

PID TTY TIME CMD 12585 pts/0 05:27:05 samp03svr bash: line 1: -o: command not found

My code:

include('Net/SSH2.php');

$cmd = "sudo ps -p {$user->sshConnect()} -o %cpu";

$ssh = new Net_SSH2('IP');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec($cmd);

sshConnect function:

public function sshConnect(){
    $connection = ssh2_connect('IP', 22);
    ssh2_auth_password($connection, 'username', 'password');
    $stream = ssh2_exec($connection, 'lsof -t -i :6000');

    stream_set_blocking($stream, true);
    $out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
    return stream_get_contents($out);
  }

But, if I replace {$user->sshConnect()} with my current PID, it works fine. Also $user->sshConnect(), return valid PID when I echo it.

Thanks.

neubert
  • 15,947
  • 24
  • 120
  • 212
milfman
  • 1
  • 4
  • It's kinda strange that you're using two different SSH libraries. I'd use one or the other - not both. Regardless... what do you get when you echo out `$cmd`? – neubert May 13 '20 at 18:56
  • 1
    I'm solved this by adding preg_replace. Because sshConnect was retrieve info with whitespace. Now works fine. – milfman May 13 '20 at 20:10

0 Answers0