问题描述
我正在开发一种工具,该工具使用 PECL SSH2 扩展通过 SSH2 从远程主机读取 iptables 配置.我能够成功连接到主机、进行身份验证并执行命令.我遇到的麻烦是有时流不包含任何数据.
I am working on a tool that reads an iptables configuration from a remote host over SSH2 using the PECL SSH2 extension. I am able to successfully make the connection to the host, authenticate, and execute commands. The trouble I am having is sometimes the stream doesn't contain any data.
/**
* 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_exec
调用更改为
About 25% of the time, loadRules()
returns false, even when connecting to locahost instead of the remote system. I was able to work around the problem by changing the ssh2_exec
call to
$stream = ssh2_exec($this->connection,"~/iptsave; sleep .5");
但我担心出了什么问题.
but I am concerned that something is wrong.
推荐答案
phpSecLib 或许可以提供帮助:
phpSecLib may be able to help:
根据这篇文章,它总是返回输出,与 ssh2.so 不同.
According to this post, it always returns the output, unlike ssh2.so.
这篇关于清空 PHP SSH2 流内容,即使使用 stream_set_blocking?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!