ssh.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. if (!DEFINED('EGP'))
  3. exit(header('Refresh: 0; URL=http://' . $_SERVER['SERVER_NAME'] . '/404'));
  4. class ssh
  5. {
  6. var $conn;
  7. var $stream;
  8. public function auth($passwd, $address)
  9. {
  10. if ($this->connect($address) and $this->auth_pwd('root', $passwd))
  11. return true;
  12. return false;
  13. }
  14. public function connect($address)
  15. {
  16. if (strpos($address, ':') !== false) {
  17. list($host, $port) = explode(':', $address);
  18. } else {
  19. $host = $address;
  20. $port = 22;
  21. }
  22. ini_set('default_socket_timeout', '3');
  23. if ($this->conn = ssh2_connect($host, $port)) {
  24. ini_set('default_socket_timeout', '180');
  25. return true;
  26. }
  27. return false;
  28. }
  29. public function setfile($localFile, $remoteFile, $permision)
  30. {
  31. if (@ssh2_scp_send($this->conn, $localFile, $remoteFile, $permision))
  32. return true;
  33. return false;
  34. }
  35. public function getfile($remoteFile, $localFile)
  36. {
  37. if (@ssh2_scp_recv($this->conn, $remoteFile, $localFile))
  38. return true;
  39. return false;
  40. }
  41. public function set($cmd)
  42. {
  43. $this->stream = ssh2_exec($this->conn, $cmd);
  44. stream_set_blocking($this->stream, true);
  45. }
  46. public function auth_pwd($u, $p)
  47. {
  48. if (@ssh2_auth_password($this->conn, $u, $p))
  49. return true;
  50. return false;
  51. }
  52. public function get($cmd = false)
  53. {
  54. if ($cmd) {
  55. $this->stream = ssh2_exec($this->conn, $cmd);
  56. stream_set_blocking($this->stream, true);
  57. }
  58. $line = '';
  59. while ($get = fgets($this->stream))
  60. $line .= $get;
  61. return $line;
  62. }
  63. public function esc()
  64. {
  65. if (function_exists('ssh2_disconnect'))
  66. ssh2_disconnect($this->conn);
  67. else {
  68. @fclose($this->conn);
  69. unset($this->conn);
  70. }
  71. return NULL;
  72. }
  73. }
  74. $ssh = new ssh;
  75. ?>