空PHP SSH2流的内容,即使有stream_set_blocking?(Empty PHP SS

2019-10-17 20:32发布

我工作的一个工具,它使用的PECL SSH2扩展了SSH2远程主机读取iptables配置。 我能够成功地使主机的连接,进行身份验证,并执行命令。 我遇到的问题是有时流不包含任何数据。

 /**
  * Load the current firewall configuration
  * @return bool
  */
 public function loadRules() {
  $stream = ssh2_exec($this->connection,"~/iptsave;");
  stream_set_blocking($stream,true);
  $iptablesSave = stream_get_contents($stream);
  if(empty($iptablesSave)) {
   return false;
   }
  parent::restore($iptablesSave);
  return true;
  }

大约25%的时间, loadRules()连接到的locahost而不是远程系统的情况下返回false。 我能够通过改变来解决该问题ssh2_exec调用

$stream = ssh2_exec($this->connection,"~/iptsave; sleep .5");

但我担心的东西是错误的。

Answer 1:

phpSecLib可能能够帮助:

根据这篇文章 ,它总是返回输出,不像ssh2.so.



Answer 2:

我在这里得到了同样的问题。 不知怎的,你需要设置延时用于获取流的结果。

这就是你所做的是可以的,但你也可以设定sleep(1)stream_set_block($stream, true)功能。 你可以尝试usleep()功能。 有没有尝试过



Answer 3:

可能这将解决这个问题:

$stream = ssh2_exec($this->connection,"~/iptsave;");
stream_set_blocking($stream,true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$iptablesSave = stream_get_contents($stream);


文章来源: Empty PHP SSH2 stream contents, even with stream_set_blocking?
标签: php ssh stream