PHP SSH API class


/ Published in: PHP
Save to your folder(s)

This SSH API is used to execute remote commands, send and receive files.


Copy this code and paste it in your HTML
  1. class SshApi extends BaseObject{
  2. public $session = null;
  3. public $authenticated = false;
  4.  
  5. public function __construct(){
  6.  
  7. }
  8.  
  9. public function connect($host){
  10. $this->session = ssh2_connect($host);
  11. return $this->session;
  12. }
  13.  
  14. public function pubkeyFile($sshUser, $sshPub, $sshPriv, $sshPass){
  15. if ( ! $this->session ) return false;
  16. if ( empty($sshPass) ){
  17. $b = ssh2_auth_pubkey_file( $this->session, $sshUser, $sshPub, $sshPriv);
  18. }
  19. else
  20. $b = ssh2_auth_pubkey_file( $this->session, $sshUser, $sshPub, $sshPriv, $sshPass);
  21. if ( $b ) $this->authenticated = true;
  22. return $b;
  23. }
  24.  
  25. public function send($localfile, $remotefile, $perms=0660){
  26. $this->debug(__METHOD__);
  27. if ( ! $this->session ) return false;
  28. $this->debug('Sending '. $localfile . ' to ' . $remotefile);
  29. $b = ssh2_scp_send($this->session, $localfile, $remotefile, $perms);
  30. return $b;
  31. }
  32.  
  33. public function recv($remotefile , $localfile){
  34. if ( ! $this->session ) return false;
  35. $b = ssh2_scp_recv($this->session , $remotefile , $localfile );
  36. return $b;
  37. }
  38.  
  39. public function exec($command){
  40. if ( ! $this->session ) return false;
  41. $stream = ssh2_exec($this->session, $command);
  42. return $stream;
  43. }
  44. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.