Guest User

Untitled

a guest
Jan 25th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. <?php
  2.  
  3. class ssh2 {
  4. const SSH_COMMAND_FINISHED_FLAG = '___SSH_COMMAND_FINISHED___';
  5.  
  6. private $host = 'localhost';
  7. private $port = 22;
  8. private $username = 'root';
  9. private $password = '';
  10. private $connectionMethods = array(
  11. 'kex' => 'diffie-hellman-group1-sha1',
  12. 'client_to_server' => array(
  13. 'crypt' => '3des-cbc',
  14. 'comp' => 'none'),
  15. 'server_to_client' => array(
  16. 'crypt' => 'aes256-cbc,aes192-cbc,aes128-cbc',
  17. 'comp' => 'none'));
  18. private $publicKeyFile;
  19. private $privateKeyFile;
  20. private $keyPassphrase;
  21. private $connection = null;
  22.  
  23. /**
  24. * @param string $host
  25. * @param int $port
  26. */
  27. public function __construct($host = 'localhost', $port = 22) {
  28. if (!function_exists("ssh2_connect")) {
  29. throw new Exception('function ssh2_connect doesn\'t exist');
  30. }
  31.  
  32. $this->setHost($host);
  33. $this->setPort($port);
  34. }
  35.  
  36. /**
  37. * @param array $connectionMethods
  38. * @return bool
  39. */
  40. public function connect($connectionMethods = false) {
  41. $this->setConnectionMethods(is_array($connectionMethods) ? $connectionMethods : $this->getConnectionMethods());
  42. $this->connection = ssh2_connect($this->getHost(), $this->getPort(), $this->getConnectionMethods());
  43. return (bool) $this->connection;
  44. }
  45.  
  46. public function disconnect() {
  47. return $this->exec('exit;', 10, false);
  48. }
  49.  
  50. /**
  51. * @return string
  52. */
  53. public function getFingerprint() {
  54. if ($this->connection) {
  55. return ssh2_fingerprint($this->connection, SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);
  56. }
  57. return false;
  58. }
  59.  
  60. /**
  61. * @param string $username
  62. * @return array
  63. */
  64. public function getAuthMethods($username) {
  65. return (array) ssh2_auth_none($this->connection, $username);
  66. }
  67.  
  68. /**
  69. * @param string $username
  70. * @param string $password
  71. * @return bool
  72. */
  73. public function authPassword($username, $password) {
  74. if ($this->connection) {
  75. $this->setUsername($username);
  76. $this->setPassword($password);
  77. return (bool) ssh2_auth_password($this->connection, $this->getUsername(), $this->getPassword());
  78. }
  79. return false;
  80. }
  81.  
  82. /**
  83. * @param string $username
  84. * @param string $publicKeyFile
  85. * @param string $privateKeyFile
  86. * @param string $keyPassphrase
  87. * @return bool
  88. */
  89. public function authKey($username, $publicKeyFile, $privateKeyFile, $keyPassphrase = null) {
  90. if ($this->connection && is_file($publicKeyFile) && is_readable($publicKeyFile) && is_file($privateKeyFile) && is_readable($privateKeyFile)) {
  91. $this->setPublicKeyFile($publicKeyFile);
  92. $this->setPrivateKeyFile($privateKeyFile);
  93. $this->setKeyPassphrase($keyPassphrase);
  94. return (bool) ssh2_auth_pubkey_file($this->connection, $this->ssh_auth_user, $this->ssh_auth_pub, $this->ssh_auth_priv, $this->ssh_auth_pass);
  95. }
  96. }
  97.  
  98. /**
  99. * @param string $cmd
  100. * @param int $timeout
  101. * @return string
  102. */
  103. public function exec($cmd, $timeout = 10, $saveMode = true) {
  104. if ($saveMode) {
  105. $cmd .= ' ; echo "' . self::SSH_COMMAND_FINISHED_FLAG . '"';
  106. }
  107.  
  108. if (!($stream = ssh2_exec($this->connection, $cmd))) {
  109. throw new Exception('SSH command failed');
  110. }
  111. stream_set_blocking($stream, true);
  112. $data = "";
  113.  
  114. $time_start = time();
  115. $data = "";
  116. if ($saveMode) {
  117. while (true) {
  118. $data .= fread($stream, 4096);
  119. $flagPosition = strpos($data, self::SSH_COMMAND_FINISHED_FLAG);
  120. if ($flagPosition !== false) {
  121. if (substr_compare($data, self::SSH_COMMAND_FINISHED_FLAG, $flagPosition, strlen(self::SSH_COMMAND_FINISHED_FLAG)) == 0) {
  122. $data = substr($data, 0, (strlen(self::SSH_COMMAND_FINISHED_FLAG) + 1) * -1);
  123. }
  124. break;
  125. }
  126. if ((time() - $time_start) > (int) $timeout) {
  127. fclose($stream);
  128. throw new Exception('ssh execution timeout');
  129. }
  130. }
  131. } else {
  132. while ($buf = fread($stream, 4096)) {
  133. $data .= $buf;
  134. }
  135. }
  136.  
  137. fclose($stream);
  138. return $data;
  139. }
  140.  
  141. /**
  142. * @param string $userPassword
  143. * @param string $cmd
  144. * @param int $timeout
  145. * @param boolean $saveMode
  146. * @return string
  147. */
  148. public function sudoExec($userPassword, $cmd, $timeout = 10, $saveMode = true) {
  149. $ssh2->exec('sudo -k', $timeout, $saveMode);
  150. return $ssh2->exec('echo "' . $userPassword . '" | sudo ' . $cmd, $timeout, $saveMode);
  151. }
  152.  
  153. public function getHost() {
  154. return $this->host;
  155. }
  156.  
  157. public function setHost($host) {
  158. $this->host = $host;
  159. }
  160.  
  161. public function getPort() {
  162. return $this->port;
  163. }
  164.  
  165. public function setPort($port) {
  166. $this->port = (int) $port;
  167. }
  168.  
  169. public function getUsername() {
  170. return $this->username;
  171. }
  172.  
  173. public function setUsername($username) {
  174. $this->username = $username;
  175. }
  176.  
  177. public function getPassword() {
  178. return $this->password;
  179. }
  180.  
  181. public function setPassword($password) {
  182. $this->password = $password;
  183. }
  184.  
  185. public function getPublicKeyFile() {
  186. return $this->publicKeyFile;
  187. }
  188.  
  189. public function setPublicKeyFile($publicKey) {
  190. $this->publicKeyFile = $publicKey;
  191. }
  192.  
  193. public function getPrivateKeyFile() {
  194. return $this->privateKeyFile;
  195. }
  196.  
  197. public function setPrivateKeyFile($privateKey) {
  198. $this->privateKeyFile = $privateKey;
  199. }
  200.  
  201. public function getKeyPassphrase() {
  202. return $this->keyPassphrase;
  203. }
  204.  
  205. public function setKeyPassphrase($keyPassphrase) {
  206. $this->keyPassphrase = $keyPassphrase;
  207. }
  208.  
  209. public function getConnectionMethods() {
  210. return $this->connectionMethods;
  211. }
  212.  
  213. public function setConnectionMethods($connectionMethods) {
  214. $this->connectionMethods = $connectionMethods;
  215. }
  216.  
  217. }
  218.  
  219. ?>
Add Comment
Please, Sign In to add comment