【问题标题】:PHP ssh2_exec returning stream, but remote command not being runPHP ssh2_exec 返回流,但远程命令未运行
【发布时间】:2015-10-22 07:52:32
【问题描述】:

我正在尝试使用 ssh2 模块在远程服务器上运行命令。日志显示身份验证成功,并且 ssh2_exec 返回一个流(记录的成功行为)。

服务器端,手动(通过 putty)以同一帐户 SSH 进入服务器允许我成功运行命令。

但是,在运行我的代码时,我的日志显示该命令甚至从未被调用过。

这是控制会话的代码:

function connect() {
    $hnd = \ssh2_connect(Config\PORTAL_SERVER);
    if (!$hnd) return false;

    if (!\ssh2_auth_password($hnd, Config\PORTAL_USER, Config\PORTAL_PASSWD))
        return false;

    return $hnd;
}

function run($api, $function, $args, $hnd) {
    $args = implode(' ', $args);
    $stream = \ssh2_exec($hnd, "api $api $function $args");
    \stream_set_blocking($stream, true);

    $response = \stream_get_contents($stream);
    fclose($stream);

    return \json_decode($response);
}

以及调用它的代码:

$hnd = Portal\connect();
$result = Portal\run(
    'users',
    'all',
    array(),
    $hnd
);

json_decode 接收一个空白字符串,因为流从不返回任何内容。我只能假设我以某种方式错误处理了我的流,但这并不能解释我的命令没有运行。

【问题讨论】:

    标签: php json ssh stream


    【解决方案1】:

    我是个白痴。

    会话开始时没有为用户正确设置路径...我切换到绝对路径,一切都很完美。

    【讨论】:

      猜你喜欢
      • 2011-08-09
      • 2019-02-15
      • 2016-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多