ssh.init.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. if (! defined('IS_INITPHP'))
  3. exit('Access Denied!');
  4. include 'ssh/Net/SSH2.php';
  5. /**
  6. * SSH登陆服务器操作
  7. *
  8. * @author Chen Deben
  9. * @copyright Meitu
  10. */
  11. class sshInit {
  12. public $ssh;
  13. /**
  14. * 登陆
  15. *
  16. * @param string $server
  17. * 服务器地址
  18. * @param string $username
  19. * 用户名
  20. * @param string $password
  21. * 密码,如果$cer=true,则此处为证书的地址
  22. * @param string $cer
  23. */
  24. public function login($server,$port=22, $username = "root", $password="", $cer = FALSE) {
  25. $this->ssh = new Net_SSH2($server,$port);
  26. if ($cer) {
  27. include ('Crypt/RSA.php');
  28. $key = new Crypt_RSA();
  29. $key->loadKey(file_get_contents($password));
  30. } else {
  31. $key = $password;
  32. }
  33. if (!$this->ssh->login($username, $key)) {
  34. return false; // Login Failed
  35. }else {
  36. return true;
  37. }
  38. }
  39. /**
  40. * 执行SSH命令
  41. */
  42. public function exec($com){
  43. return $this->ssh->exec($com);
  44. }
  45. /**
  46. * 关闭SSH连接
  47. */
  48. public function close(){
  49. return $this->ssh->disconnect();
  50. }
  51. }