host = $host; $this->port = $port; $this->connection(); } ######################################################################## ####### CONEXAO COM SERVIDOR ####### ######################################################################## /** * Realiza a conexao inicial com o servidor * * @return boolean */ private function connection() { $this->connection = ssh2_connect($this->host, $this->port); if (!$this->connection) { return false; } return true; } /** * Autentica usuario e senha com a conexao criada * * @param string $login * @param string $password * @return object */ public function connectionAuth($login, $password) { if(ssh2_auth_password($this->connection, $login, $password)){ return true; } else { return false; } } /** * Autentica a conexao como root no sistema. * @param string $passwd */ public function connectionRoot($passwd) { $this->stream = ssh2_shell($this->connection); $this->command(self::CONF_ROOT_LOGIN); $this->command($passwd); $str = $this->command(PHP_EOL); if(strpos($str,"incorrect password") === false){ return true; } else { return false; } } /** * Fecha a conexao com o servidor. */ public function connectionClose() { unset($this->connection); fclose($this->stream); } ######################################################################## ####### CONEXAO COM SERVIDOR ####### ######################################################################## /** * Executa comando pela conexao criada. * @param string $command * @return boolean|string */ public function command($command) { $this->command = $command; if ($this->stream) { fwrite($this->stream, $this->command . PHP_EOL); return $this->console(); } } /** * Apresenta apenas o resultado do comando executado; * @return string */ public function result() { if (!$this->terminal) { fwrite($this->stream, "\n"); $console = $this->console; return str_replace($this->terminal($this->console()), "", $console); } return str_replace($this->terminal(), "", $this->console); } /** * Apresenta todas as informacoes dos comandos executados. * @param boolean * @return string */ private function console($clean = true) { if ($clean) { unset($this->console); } sleep(self::CONF_TIME_EXEC); $this->console = stream_get_contents($this->stream); return $this->console; } /** * Apresenta o terminal sendo executado; * @param string $terminal * @return string */ private function terminal($terminal) { if (!$this->terminal) { $this->terminal = $terminal; } return $this->terminal; } } ?>