PHP ssh2_exec流挂起
我一直在尝试让PHP ssh2_exec流挂起,php,ssh,libssh2,Php,Ssh,Libssh2,我一直在尝试让ssh2\u exec运行并从远程主机返回响应,但无法找到正确的方法。我根据其他人的建议将此函数组合在一起,但一旦到达stream\u get\u contents($errorStream),该函数总是挂起 我正在运行的命令是ls-l,因此它应该执行得非常快 public function exec($command) { $stream = ssh2_exec($this->ssh, $command); if (! $stream) {
ssh2\u exec
运行并从远程主机返回响应,但无法找到正确的方法。我根据其他人的建议将此函数组合在一起,但一旦到达stream\u get\u contents($errorStream),该函数总是挂起代码>
我正在运行的命令是ls-l
,因此它应该执行得非常快
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()将挂起
一种避免的方法是使用:stream\u set\u timeout()
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->exec('ls -l');
$stream = ssh2_exec($this->ssh, $command);
if (! $stream) {
throw new exception('Could not open shell exec stream');
}
stream_set_timeout($stream, 10);