classe.ssh.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. class SSH2 {
  3. var $ssh;
  4. var $stream;
  5. function __construct($host, $port=22) {
  6. if (!$this->ssh = ssh2_connect($host, $port)) {
  7. return false;
  8. }
  9. }
  10. function online($host) {
  11. $port=22;
  12. if (!$this->ssh = ssh2_connect($host, $port)) {
  13. return false;
  14. }else{
  15. return true;
  16. }
  17. }
  18. function auth($username, $auth, $private = null, $secret = null) {
  19. if(is_file($auth) && is_readable($auth) && isset($private)) {
  20. // If $auth is a file, and $private is set, try pubkey auth
  21. if(!ssh2_auth_pubkey_file($this->ssh, $username, $auth, $private, $secret)) {
  22. return false;
  23. }
  24. } else {
  25. // If not pubkey auth, auth with password
  26. if(!ssh2_auth_password($this->ssh, $username, $auth)) {
  27. return false;
  28. }
  29. }
  30. return true;
  31. }
  32. function send($local, $remote, $perm) {
  33. if(!ssh2_scp_send($this->ssh, $local, $remote, $perm)) {
  34. return false;
  35. }
  36. return true;
  37. }
  38. function get($remote, $local) {
  39. if(ssh2_scp_recv($this->ssh, $remote, $local)) {
  40. return false;
  41. }
  42. return true;
  43. }
  44. function cmd($cmd, $blocking = true) {
  45. $this->stream = ssh2_exec($this->ssh, $cmd);
  46. stream_set_blocking($this->stream, $blocking);
  47. }
  48. // Just an aliasfunction for $this->cmd
  49. function exec($cmd, $blocking = true) {
  50. $this->cmd($cmd, $blocking = true);
  51. }
  52. function output() {
  53. return stream_get_contents($this->stream);
  54. }
  55. }