Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
空的PHP SSH2流内容,即使流被阻塞?_Php_Ssh_Stream - Fatal编程技术网

空的PHP SSH2流内容,即使流被阻塞?

空的PHP SSH2流内容,即使流被阻塞?,php,ssh,stream,Php,Ssh,Stream,我正在开发一个工具,使用PECL SSH2扩展从远程主机通过SSH2读取iptables配置。我能够成功地连接到主机、进行身份验证并执行命令。我遇到的问题是,有时流不包含任何数据 /** * Load the current firewall configuration * @return bool */ public function loadRules() { $stream = ssh2_exec($this->connection,"~/iptsave;");

我正在开发一个工具,使用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()
返回false,即使连接到locahost而不是远程系统。通过将
ssh2\u exec
调用更改为

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

但我担心出了问题。

我这里也有同样的问题。不知何故,您需要设置一个延迟来获得流的结果

这样做是可能的,但也可以在
stream\u set\u块($stream,true)
函数之后设置
sleep(1)

您可以尝试使用
usleep()
函数。尚未尝试过它

phpSecLib可能会提供帮助:


根据,它总是返回输出,与ssh2不同。因此。

这可能会解决问题:

$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);

你有没有得到你问题的答案?我以为这些解决方案不起作用,但我发现了我的错误。我附加到命令“2>&1”字符串的末尾,以便将stderr重定向到stdout。现在它可以工作了:)请查看此链接了解更多信息:我认为这些解决方案不起作用,但我发现了我的错误。我附加到命令“2>&1”字符串的末尾,以便将stderr重定向到stdout。现在可以工作了:)有关详细信息,请查看此链接: