PHP ssh2_exec流挂起 [英] PHP ssh2_exec stream hanging

查看:310
本文介绍了PHP ssh2_exec流挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让ssh2_exec运行并从远程主机返回响应,但是无法找出执行此操作的正确方法.我根据其他人的建议一起使用了该功能,但是一旦到达stream_get_contents($errorStream);,该功能就会一直挂起.

I've been trying to get ssh2_exec to run and return the response from the remote host, but cannot figure out the proper way to do this. I thew this function together based on what others have recommended, but the function always hangs once it gets to stream_get_contents($errorStream);.

我正在运行的命令是ls -l,因此它应该很快执行.

The command I'm running is ls -l so it should be executing very quickly.

public function exec($command) 
{
    $stream = ssh2_exec($this->ssh, $command);

    if (! $stream) {
        throw new exception('Could not open shell exec stream');
    }
    $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);

    stream_set_blocking($errorStream, true);
    stream_set_blocking($stream, true);

    $err      = stream_get_contents($errorStream);
    $response = stream_get_contents($stream);

    @fclose($errorStream);
    @fclose($stream);

    if ($err) {
        throw new exception($err);
    }

    return $response;
}

推荐答案

我发现,如果命令输出的大小达到64KB(恰好是我的Linux开发箱上的那个数字),函数ssh2_exec()将挂起.

I found that the function ssh2_exec() will hang if the size of the output of the command reaches to 64KB (exactly that number on my Linux dev box).

避免使用的一种方法是使用:stream_set_timeout()

One way to avoid is to use: stream_set_timeout()

$stream = ssh2_exec($this->ssh, $command);

if (! $stream) {
    throw new exception('Could not open shell exec stream');
}

stream_set_timeout($stream, 10);

这篇关于PHP ssh2_exec流挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆