Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.13 KB | None | 0 0
  1. <?php
  2. namespace App\IO;
  3.  
  4. use App\Servers;
  5. use App\IO\Dialplan;
  6.  
  7. class SSH {
  8.     // SSH Host
  9.     private $ssh_host = 'myserver.example.com';
  10.     // SSH Port
  11.     private $ssh_port = 1987;
  12.     // SSH Server Fingerprint
  13.     private $ssh_server_fp = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  14.     // SSH Username
  15.     private $ssh_auth_user = 'root';
  16.     // SSH Public Key File
  17. //    private $ssh_auth_pub = '/var/www/ssl/unlock.pub';
  18.     private $ssh_auth_pub = '/var/www/ssl/id_rsa.pub';
  19.     // SSH Private Key File
  20.     private $ssh_auth_priv = '/var/www/ssl/id_rsa';
  21.     // SSH Private Key Passphrase (null == no passphrase)
  22.     private $ssh_auth_pass;
  23.     // SSH Connection
  24.     private $connection;
  25.    
  26.     public function connect($auth_data = false) {
  27.  
  28.         $this->ssh_host         = $auth_data['host'];
  29.         $this->ssh_auth_pass    = $auth_data['password'];
  30.         $this->ssh_port         = $auth_data['port'];
  31.        
  32.         if (!($this->connection = ssh2_connect($this->ssh_host, $this->ssh_port))) {
  33.             return [
  34.                 'error_no'  => 1,
  35.                 'msg'       => 'Cannot connect to server'
  36.             ];
  37.             throw new \Exception('Cannot connect to server');
  38.         }
  39.         $fingerprint = ssh2_fingerprint($this->connection, SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);
  40.         if (strcmp($fingerprint, $fingerprint) !== 0) {
  41.             return [
  42.                 'error_no'  => 1,
  43.                 'msg'       => 'Unable to verify server identity!'
  44.             ];
  45.             throw new \Exception('Unable to verify server identity!');
  46.         }
  47.         if (!ssh2_auth_pubkey_file($this->connection, $this->ssh_auth_user, $this->ssh_auth_pub, $this->ssh_auth_priv, $this->ssh_auth_pass)) {
  48.             return [
  49.                 'error_no'  => 1,
  50.                 'msg'       => 'Autentication rejected by server'
  51.             ];
  52.             throw new \Exception('Autentication rejected by server');
  53.         }
  54.        
  55.         return [
  56.             'error_no'  => 0,
  57.             'msg'       => 'Connected'
  58.         ];
  59.     }
  60.    
  61.     public function exec($cmd) {
  62.         if (!($stream = ssh2_exec($this->connection, $cmd))) {
  63.             throw new \Exception('SSH command failed');
  64.         }
  65.         stream_set_blocking($stream, true);
  66.         $data = "";
  67.         while ($buf = fread($stream, 4096)) {
  68.             $data .= $buf;
  69.         }
  70.         fclose($stream);
  71.         return $data;
  72.     }
  73.    
  74.     public function sendFile($local_file_path = false, $remote_file_path = false) {
  75.         if($local_file_path && $remote_file_path && $this->connection) {
  76.            
  77.             $status = ssh2_scp_send($this->connection, $local_file_path, $remote_file_path, 0644);
  78.            
  79.             return $status;
  80.         } else {
  81.             return false;
  82.         }
  83.     }
  84.    
  85.     public function disconnect() {
  86.         $this->connection = null;
  87.     }
  88.    
  89.     public function isConnected() {
  90.         if($this->connection) {
  91.             return true;
  92.         } else {
  93.             return false;
  94.         }
  95.     }
  96.    
  97.     public function __destruct() {
  98.         $this->disconnect();
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement