龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

ssh php实现的ssh api类

时间:2014-06-18 02:13来源:网络整理 作者:网络 点击:
分享到:
php实现的ssh api类 [代码片段(44行)]
class SshApi extends BaseObject{
   public $session = null;
   public $authenticated = false;

   public function __construct(){

   }

   public function connect($host){
      $this->session = ssh2_connect($host);
      return $this->session;
   }

   public function pubkeyFile($sshUser, $sshPub, $sshPriv, $sshPass){
      if ( ! $this->session ) return false;
      if ( empty($sshPass) ){
         $b = ssh2_auth_pubkey_file( $this->session, $sshUser, $sshPub, $sshPriv);
      }
      else
         $b = ssh2_auth_pubkey_file( $this->session, $sshUser, $sshPub, $sshPriv, $sshPass);
      if ( $b ) $this->authenticated = true;
      return $b;
   }

   public function send($localfile, $remotefile, $perms=0660){
      $this->debug(__METHOD__);
      if ( ! $this->session ) return false;
      $this->debug('Sending '. $localfile . ' to ' . $remotefile);
      $b = ssh2_scp_send($this->session, $localfile, $remotefile, $perms);      
      return $b;
   }

   public function recv($remotefile , $localfile){
      if ( ! $this->session ) return false;
      $b = ssh2_scp_recv($this->session , $remotefile , $localfile );
      return $b;
   }

   public function exec($command){
      if ( ! $this->session ) return false;   
      $stream = ssh2_exec($this->session, $command);
      return $stream;
   }
}
精彩图集

赞助商链接