首页 > 解决方案 > ssh2_exec 中的 PHP 变量

问题描述

操作系统:Ubuntu 16.04

我尝试在 ssh2_exec 中使用变量,但收到以下消息:

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

我的代码:

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 功能:

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);
  }

但是,如果我用我当前的 PID 替换 {$user->sshConnect()},它就可以正常工作。还有 $user->sshConnect(),当我回显它时返回有效的 PID。

谢谢。

标签: phplinuxphpseclibpsssh2

解决方案


推荐阅读