123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <?
- class SSH {
-
- public $authed = false;
- public $fingerprint;
- public $cmd_delay = 1;
- public $cmd_exit = "exit";
- public $cmd_exit_echo = true;
- private $connection;
- private $stream;
-
-
-
- public function __destruct() {
-
- $this->Disconnect();
- }
-
-
-
- public function Connect($host, $port, $fingerprint = "", $username = "", $password = "", $public_key_path = "", $private_key_path = "") {
- $this->Authed = false;
-
- if (!($this->connection = @ssh2_connect($host, $port))) {
- throw new Exception("Could not connect to server.");
- }
-
- $this->fingerprint = ssh2_fingerprint($this->connection, SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);
- if ($fingerprint) {
- if (strcmp($this->fingerprint, $fingerprint) !== 0) {
- throw new Exception("Unable to verify server identity.");
- }
- }
-
- if ($username && $public_key_path && $private_key_path) {
- $this->Authed = $this->AuthByKey($username, $public_key_path, $private_key_path);
- }
- elseif ($username && $password) {
- $this->Authed = $this->AuthByPassword($username, $password);
- }
- }
-
-
-
- public function Disconnect($run_exit_cmd = true) {
-
- $response = "";
-
- if ($this->stream) {
- if ($run_exit_cmd && $this->cmd_exit) {
- $response = $this->Exec($this->cmd_exit, 1);
- }
- fclose($this->stream);
- }
- elseif ($run_exit_cmd && $this->authed) {
- $response = $this->Exec($this->cmd_exit);
- }
-
- if ($this->cmd_exit_echo) {
- echo $response;
- }
-
- $this->authed = false;
- $this->stream = null;
- $this->connection = null;
- }
-
-
-
- public function AuthByPassword($username, $password) {
- if ($this->connection) {
- if(!$this->authed = @ssh2_auth_password($this->connection, $username, $password)) {
- throw new Exception("Autentication rejected by server.");
- }
- }
- }
-
-
-
- public function AuthByKey($username, $public_key_path, $private_key_path) {
-
- if ($this->connection) {
- if (!file_exists($public_key_path)) {
- throw new Exception("Failed to find public key.");
- }
- elseif (!file_exists($private_key_path)) {
- throw new Exception("Failed to find private key.");
- }
- elseif (!is_readable($public_key_path)) {
- throw new Exception("Failed to read public key.");
- }
- elseif (!is_readable($private_key_path)) {
- throw new Exception("Failed to read private key.");
- }
- elseif (!$this->authed = @ssh2_auth_pubkey_file($this->connection, $username, $public_key_path, $private_key_path)) {
- throw new Exception("Autentication rejected by server.");
- }
- }
- }
-
-
-
- public function ReadStream($buffer_size = 4096) {
-
- if ($this->stream) {
- $data = fread($this->stream, $buffer_size);
- }
- else {
- $data = false;
- }
-
- return $data;
- }
-
-
-
- public function Exec($cmd, $delay = -1) {
-
- $data = "";
-
- if ($this->stream) {
-
- fwrite($this->stream, $cmd.PHP_EOL);
-
- if ($delay != 0) {
- sleep(($delay == -1 ? $this->cmd_delay : $delay));
- }
-
- while ($buf = fread($this->stream, 4096)) {
- $data .= $buf;
- }
- }
- else {
- if (!($stream = @ssh2_exec($this->connection, $cmd))) {
- throw new exception("Command failed.");
- }
-
- $err_stream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
- $io_stream = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
- stream_set_blocking($err_stream, true);
- stream_set_blocking($io_stream, true);
-
- while ($buf = fread($err_stream, 4096)) {
- $data .= $buf;
- }
-
- while ($buf = fread($io_stream, 4096)) {
- $data .= $buf;
- }
- fclose($stream);
- }
-
- return $data;
- }
-
-
-
- public function Shell($term_type = "vt102", $delay = -1) {
-
- $data = "";
-
- if (!($this->stream = @ssh2_shell($this->connection, $term_type))) {
- throw new exception("Shell failed.");
- }
-
- if ($delay != 0) {
- sleep(($delay == -1 ? $this->cmd_delay : $delay));
- }
-
- while ($buf = fread($this->stream, 4096)) {
- $data .= $buf;
- }
-
- return $data;
- }
- }
- ?>
|