[英]PHP ssh2_exec stream hanging
我一直試圖讓ssh2_exec
運行並從遠程主機返回響應,但無法找出執行此操作的正確方法。 我根據其他人的建議將該函數放在一起,但是一旦到達stream_get_contents($errorStream);
,該函數始終掛起stream_get_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_set_timeout()
$stream = ssh2_exec($this->ssh, $command);
if (! $stream) {
throw new exception('Could not open shell exec stream');
}
stream_set_timeout($stream, 10);
老實說,我將使用phpseclib,這是一個純PHP SSH實現 。 例如。
<?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');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.