forked from SimplesIP/pabx-app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
4.2 KiB
149 lines
4.2 KiB
2 years ago
|
<?php
|
||
|
|
||
|
/**
|
||
|
* Classe para fazer conexao com servidor via SSH
|
||
|
* e executar comandos como root.
|
||
|
*
|
||
|
* @author Lucas Awade
|
||
|
* @date 25/01/2020
|
||
|
*/
|
||
|
class SSH {
|
||
|
|
||
|
/** @connections */
|
||
|
private $connection;
|
||
|
private $host;
|
||
|
private $port;
|
||
|
|
||
|
/** @streming */
|
||
|
private $stream;
|
||
|
|
||
|
/** @server */
|
||
|
private $console;
|
||
|
private $terminal;
|
||
|
private $command;
|
||
|
|
||
|
/** @constantes */
|
||
|
const CONF_ROOT_LOGIN = "su";
|
||
|
const CONF_TIME_EXEC = 2;
|
||
|
|
||
|
function __construct($host, $port) {
|
||
|
$this->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;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
?>
|