Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\IO;
- use App\Servers;
- use App\IO\Dialplan;
- class SSH {
- // SSH Host
- private $ssh_host = 'myserver.example.com';
- // SSH Port
- private $ssh_port = 1987;
- // SSH Server Fingerprint
- private $ssh_server_fp = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
- // SSH Username
- private $ssh_auth_user = 'root';
- // SSH Public Key File
- // private $ssh_auth_pub = '/var/www/ssl/unlock.pub';
- private $ssh_auth_pub = '/var/www/ssl/id_rsa.pub';
- // SSH Private Key File
- private $ssh_auth_priv = '/var/www/ssl/id_rsa';
- // SSH Private Key Passphrase (null == no passphrase)
- private $ssh_auth_pass;
- // SSH Connection
- private $connection;
- public function connect($auth_data = false) {
- $this->ssh_host = $auth_data['host'];
- $this->ssh_auth_pass = $auth_data['password'];
- $this->ssh_port = $auth_data['port'];
- if (!($this->connection = ssh2_connect($this->ssh_host, $this->ssh_port))) {
- return [
- 'error_no' => 1,
- 'msg' => 'Cannot connect to server'
- ];
- throw new \Exception('Cannot connect to server');
- }
- $fingerprint = ssh2_fingerprint($this->connection, SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);
- if (strcmp($fingerprint, $fingerprint) !== 0) {
- return [
- 'error_no' => 1,
- 'msg' => 'Unable to verify server identity!'
- ];
- throw new \Exception('Unable to verify server identity!');
- }
- if (!ssh2_auth_pubkey_file($this->connection, $this->ssh_auth_user, $this->ssh_auth_pub, $this->ssh_auth_priv, $this->ssh_auth_pass)) {
- return [
- 'error_no' => 1,
- 'msg' => 'Autentication rejected by server'
- ];
- throw new \Exception('Autentication rejected by server');
- }
- return [
- 'error_no' => 0,
- 'msg' => 'Connected'
- ];
- }
- public function exec($cmd) {
- if (!($stream = ssh2_exec($this->connection, $cmd))) {
- throw new \Exception('SSH command failed');
- }
- stream_set_blocking($stream, true);
- $data = "";
- while ($buf = fread($stream, 4096)) {
- $data .= $buf;
- }
- fclose($stream);
- return $data;
- }
- public function sendFile($local_file_path = false, $remote_file_path = false) {
- if($local_file_path && $remote_file_path && $this->connection) {
- $status = ssh2_scp_send($this->connection, $local_file_path, $remote_file_path, 0644);
- return $status;
- } else {
- return false;
- }
- }
- public function disconnect() {
- $this->connection = null;
- }
- public function isConnected() {
- if($this->connection) {
- return true;
- } else {
- return false;
- }
- }
- public function __destruct() {
- $this->disconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement