ssh.php 2.2 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. list($host, $port) = explode(':', $address);
  17. if($port == '')
  18. $port = 22;
  19. ini_set('default_socket_timeout', '3');
  20. if($this->conn = ssh2_connect($host, $port))
  21. {
  22. ini_set('default_socket_timeout', '180');
  23. return true;
  24. }
  25. return false;
  26. }
  27. public function setfile($localFile, $remoteFile, $permision)
  28. {
  29. if(@ssh2_scp_send($this->conn, $localFile, $remoteFile, $permision))
  30. return true;
  31. return false;
  32. }
  33. public function getfile($remoteFile, $localFile)
  34. {
  35. if(@ssh2_scp_recv($this->conn, $remoteFile, $localFile))
  36. return true;
  37. return false;
  38. }
  39. public function set($cmd)
  40. {
  41. $this->stream = ssh2_exec($this->conn, $cmd);
  42. stream_set_blocking($this->stream, true);
  43. }
  44. public function auth_pwd($u, $p)
  45. {
  46. if(@ssh2_auth_password($this->conn, $u, $p))
  47. return true;
  48. return false;
  49. }
  50. public function get($cmd = false)
  51. {
  52. if($cmd)
  53. {
  54. $this->stream = ssh2_exec($this->conn, $cmd);
  55. stream_set_blocking($this->stream, true);
  56. }
  57. $line = '';
  58. while($get = fgets($this->stream))
  59. $line.= $get;
  60. return $line;
  61. }
  62. public function esc()
  63. {
  64. if(function_exists('ssh2_disconnect'))
  65. ssh2_disconnect($this->conn);
  66. else{
  67. @fclose($this->conn);
  68. unset($this->conn);
  69. }
  70. return NULL;
  71. }
  72. }
  73. $ssh = new ssh;
  74. ?>