Veruse

Untitled

Sep 8th, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. <?php
  2.  
  3. class Components_Ssh {
  4.  
  5. private $host;
  6.  
  7. private $user;
  8.  
  9. private $pass;
  10.  
  11. private $port;
  12.  
  13. private $conn = false;
  14.  
  15. private $error;
  16.  
  17. private $stream;
  18.  
  19. private $stream_timeout = 100;
  20.  
  21. private $log;
  22.  
  23. private $lastLog;
  24.  
  25. public function __construct ( $host, $user, $pass, $port, $serverLog ) {
  26. $this->host = $host;
  27. $this->user = $user;
  28. $this->pass = $pass;
  29. $this->port = $port;
  30. $this->sLog = $serverLog;
  31.  
  32. if ( $this->connect ()->authenticate () ) {
  33. return true;
  34. }
  35. }
  36.  
  37. public function isConnected () {
  38. return ( boolean ) $this->conn;
  39. }
  40.  
  41. public function __get ( $name ) {
  42. return $this->$name;
  43. }
  44.  
  45. public function connect () {
  46. $this->logAction ( "Connecting to {$this->host}" );
  47. if ( $this->conn = ssh2_connect ( $this->host, $this->port ) ) {
  48. return $this;
  49. }
  50. $this->logAction ( "Connection to {$this->host} failed" );
  51. throw new Exception ( "Unable to connect to {$this->host}" );
  52. }
  53.  
  54. public function authenticate () {
  55. $this->logAction ( "Authenticating to {$this->host}" );
  56. if ( ssh2_auth_password ( $this->conn, $this->user, $this->pass ) ) {
  57. return $this;
  58. }
  59. $this->logAction ( "Authentication to {$this->host} failed" );
  60. throw new Exception ( "Unable to authenticate to {$this->host}" );
  61. }
  62.  
  63. public function sendFile ( $localFile, $remoteFile, $permision = 0644 ) {
  64. if ( ! is_file ( $localFile ) ) throw new Exception ( "Local file {$localFile} does not exist" );
  65. $this->logAction ( "Sending file $localFile as $remoteFile" );
  66.  
  67. $sftp = ssh2_sftp ( $this->conn );
  68. $sftpStream = @fopen ( 'ssh2.sftp://' . $sftp . $remoteFile, 'w' );
  69. if ( ! $sftpStream ) {
  70. // if 1 method failes try the other one
  71. if ( ! @ssh2_scp_send ( $this->conn, $localFile, $remoteFile, $permision ) ) {
  72. throw new Exception ( "Could not open remote file: $remoteFile" );
  73. }
  74. else {
  75. return true;
  76. }
  77. }
  78.  
  79. $data_to_send = @file_get_contents ( $localFile );
  80.  
  81. if ( @fwrite ( $sftpStream, $data_to_send ) === false ) {
  82. throw new Exception ( "Could not send data from file: $localFile." );
  83. }
  84.  
  85. fclose ( $sftpStream );
  86.  
  87. $this->logAction ( "Sending file $localFile as $remoteFile succeeded" );
  88. return true;
  89. }
  90.  
  91. public function getFile ( $remoteFile, $localFile ) {
  92. $this->logAction ( "Receiving file $remoteFile as $localFile" );
  93. if ( ssh2_scp_recv ( $this->conn, $remoteFile, $localFile ) ) {
  94. return true;
  95. }
  96. $this->logAction ( "Receiving file $remoteFile as $localFile failed" );
  97. throw new Exception ( "Unable to get file to {$remoteFile}" );
  98. }
  99.  
  100. public function cmd ( $cmd, $returnOutput = false ) {
  101. $this->logAction ( "Executing command $cmd" );
  102. $this->stream = ssh2_exec ( $this->conn, $cmd );
  103.  
  104. if ( FALSE === $this->stream ) {
  105. $this->logAction ( "Unable to execute command $cmd" );
  106. throw new Exception ( "Unable to execute command '$cmd'" );
  107. }
  108. $this->logAction ( "$cmd was executed" );
  109.  
  110. stream_set_blocking ( $this->stream, true );
  111. stream_set_timeout ( $this->stream, $this->stream_timeout );
  112. $this->lastLog = stream_get_contents ( $this->stream );
  113.  
  114. $this->logAction ( "$cmd output: {$this->lastLog}" );
  115. fclose ( $this->stream );
  116. $this->log .= $this->lastLog . "\n";
  117. return ( $returnOutput ) ? $this->lastLog : $this;
  118. }
  119.  
  120. public function shellCmd ( $cmds = array () ) {
  121. $this->logAction ( "Openning ssh2 shell" );
  122. $this->shellStream = ssh2_shell ( $this->conn );
  123.  
  124. sleep ( 1 );
  125. $out = '';
  126. while ( $line = fgets ( $this->shellStream ) ) {
  127. $out .= $line;
  128. }
  129.  
  130. $this->logAction ( "ssh2 shell output: $out" );
  131.  
  132. foreach ( $cmds as $cmd ) {
  133. $out = '';
  134. $this->logAction ( "Writing ssh2 shell command: $cmd" );
  135. fwrite ( $this->shellStream, "$cmd" . PHP_EOL );
  136. sleep ( 1 );
  137. while ( $line = fgets ( $this->shellStream ) ) {
  138. $out .= $line;
  139. sleep ( 1 );
  140. }
  141. $this->logAction ( "ssh2 shell command $cmd output: $out" );
  142. }
  143.  
  144. $this->logAction ( "Closing shell stream" );
  145. fclose ( $this->shellStream );
  146. }
  147.  
  148. public function getLastOutput () {
  149. return $this->lastLog;
  150. }
  151.  
  152. public function getOutput () {
  153. return $this->log;
  154. }
  155.  
  156. public function disconnect () {
  157. $this->logAction ( "Disconnecting from {$this->host}" );
  158. // if disconnect function is available call it..
  159. if ( function_exists ( 'ssh2_disconnect' ) ) {
  160. ssh2_disconnect ( $this->conn );
  161. }
  162. else { // if no disconnect func is available, close conn, unset var
  163. @fclose ( $this->conn );
  164. $this->conn = false;
  165. }
  166. // return null always
  167. return NULL;
  168. }
  169.  
  170. public function fileExists ( $path ) {
  171. $output = $this->cmd ( "[ -f $path ] && echo 1 || echo 0", true );
  172. return ( bool ) trim ( $output );
  173. }
  174. }
Add Comment
Please, Sign In to add comment