1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
<?php
class SSH {
private $conn; // Connection
private $log=array(); // Event log
private $command=NULL;
private $stream;
private $debug=false;
public $data;
public $fingerprint;
public $console=array();
public function __construct($user, $pass, $host, $port=22) {
if ($this->connect($host,$port))
{
if (@$this->auth_pwd($user,$pass)){
return true;
} else {
return false;
}
} else {
return false;
}
}
/*** Connection SSH ***/
public function connect($host,$port=22) {
if ($this->conn = ssh2_connect($host, $port))
{
$this->fingerprint=@ssh2_fingerprint($connection,SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);
$this->log[] = 'Connection succes to '.$host.':'.$port;
return true;
} else {
$this->log[] = 'Connection faild to '.$host.':'.$port;
return false;
}
}
/*** Login SSH ***/
public function auth_pwd($user,$pass) {
if (ssh2_auth_password($this->conn, $user, $pass)==true)
{
$this->log[] = 'Login succces by '.$user;
return true;
} else {
$this->log[] = 'Login faild by '.$user;
return false;
}
}
/*** Open shell ***/
public function shell($term_type=SSH2_DEFAULT_TERMINAL, $env=NULL, $width=SSH2_DEFAULT_TERM_WIDTH, $height=SSH2_DEFAULT_TERM_HEIGHT) {
if ($this->stream = @ssh2_shell($this->conn, $term_type, $env, $width, $height, SSH2_DEFAULT_TERM_UNIT))
{
$this->log[] = 'Open shell with PARAM terminal = ' . $term_type . ' env = ' .$env . ' width =' .$width . ' height = ' .$height;
if ($this->stream!=false) {
return true;
}
else
{
$this->log[] = 'Open shell not run. Exit';
$this->__destruct();
return false;
}
}
else
{
return false;
}
}
/*** Run a command ***/
public function cmd($cmd, $toudou=false) {
if ($toudou == true)
{
if (!empty($this->stream))
{
$this->log[] = 'Run cmd : ' . $cmd;
//fwrite($this->stream, $cmd . PHP_EOL);
$stream = ssh2_exec($this->conn, $cmd . PHP_EOL);
sleep ( 1 );
stream_set_blocking($stream, true);
// The command may not finish properly if the stream is not read to end
$this->console[] = stream_get_contents($stream);
}
else
{
$this->log[] = 'Shell not run : exit';
$this->__destruct();
}
}
else
{
$this->log[] = 'add command to list : '. $cmd;
$this->command.=$cmd . ' ; ';
}
}
public function shell_cmd($cmd)
{
fwrite($this->stream, $cmd . PHP_EOL);
}
/*** Return log debuging ***/
function log($print=false) {
if ($print == false)
{
return $this->log;
}
else
{
echo '<pre>';
foreach ($this->log as $key => $value)
{
echo $key . ' ' . $value . PHP_EOL;
}
echo '</pre>';
}
}
/*** Print terminal return ***/
public function get() {
while($line = fgets($this->stream, 8192)) { flush(); echo $line . PHP_EOL;}
}
public function console(){
$this->log[] = 'Send command list : ' .$this->command;
$this->cmd($this->command, true);
$this->disconnect();
return $this->console;
}
/*** Deconnexion ***/
public function disconnect() {
// if disconnect function is available call it..
if ( function_exists('ssh2_disconnect') ) {
$this->log[] = 'Disconnect by funct ssh2_disconnect';
ssh2_disconnect($this->conn);
} else { // if no disconnect func is available, close conn, unset var
$this->log[] = 'Disconnect by fclose';
@fclose($this->conn);
unset($this->conn);
}
if ($this->debug == true){$this->log(true);}
// return null always
return NULL;
}
/*** Destruction de class ***/
public function __destruct() {
// fermeture de la connection, bool envois direct
$this->cmd('echo "Exit" ; exit', true);
$this->disconnect();
}
}
?> |
Partager