我工作的一个工具,它使用的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");
但我担心的东西是错误的。