Penggunaan fungsi SSH2 pada PHP

Penggunaan fungsi SSH2 pada PHP

Halo! Pada kesempatan ini saya akan memberikan tutorial mengaktifkan fitur SSH2 pada cPanel Hosting. SSH2 adalah suatu extensi PHP yang memungkinkan kita untuk mengakses SSH dari sebuah server melalui script atau perintah PHP. Dengan demikian kita bisa mengeksekusi perintah shell melalui PHP. Fitur ini mulai tersedia pada PHP versi 7.

Manfaat Mengaktifkan Fitur SSH2 Pada cPanel Hosting

Banyak kegunaan yang dapat kita peroleh melalui fitur SSH2 ini. Pertama, kita dapat mengakses server kita dengan SSH melalui script PHP. Kedua, kita dapat mengeksekusi perintah-perintah shell melalui script PHP. Selain itu, extension dibutuhkan untuk mayoritas website SSH Reseller Panel atau OCS panel.

Jadi apabila Anda hendak menginstall OCS panel di hosting, atau website lainnya yang membutuhkan SSH2, kita harus mengaktifkan fitur SSH2 terlebih dahulu pada cPanel Hosting Kita.

Pada umumnya, extension SSH2 pada cPanel hosting dalam kondisi disabled. Oleh karena itu kita harus mengaktifkannya terlebih dahulu. Hal yang perlu diperhatikan adalah tidak semua hosting provider mengizinkan fitur ini. Tetapi, jika Anda beli hosting dari Hosting Termurah, fitur ini dapat diaktifkan.

Tutorial Mengaktifkan Fitur SSH2 Pada cPanel Hosting

  1. Login ke cPanel dari hosting Anda
  2. Klik pada icon/tulisan Select PHP Version (di kategori Software)
  3. Pada dropdown, pilih versi 7.0, maka akan muncul list extension yang dapat diaktifkan
  4. Tekan tombol Use Defaults sehingga module-module default akan tercentang
  5. Centang extension SSH2, lalu klik tombol Save
  6. Tekan tombol Set As Current
    Penggunaan fungsi SSH2 pada PHP

    (Klik pada gambar untuk memperbesar. Mohon perhatikan angka yang tertulis pada gambar)

Demikianlah tutorial mengaktifkan fitur SSH2 pada cPanel hosting. Semoga tutorial ini dapat membantu Anda dalam mengaktifkan SSh2 pada hosting. Apabila Anda mempunyai pertanyaan, Anda dapat menanyakannya melalui kolom komentar di bawah. Terima kasih

Saya mengalami kesulitan dengan ssh2 di php sebagian besar karena aliran output kadang-kadang berfungsi dan kadang tidak. Saya hanya akan menempelkan lib saya di sini yang bekerja dengan baik untuk saya. Jika ada ketidakkonsistenan kecil dalam kode itu karena saya sudah terpasang dalam suatu kerangka kerja tetapi Anda harus porting baik-baik saja:

<?php

class Components_Ssh {

    private $Host;

    private $user;

    private $pass;

    private $port;

    private $conn = false;

    private $error;

    private $stream;

    private $stream_timeout = 100;

    private $log;

    private $lastLog;

    public function __construct ( $Host, $user, $pass, $port, $serverLog ) {
        $this->Host = $Host;
        $this->user = $user;
        $this->pass = $pass;
        $this->port = $port;
        $this->sLog = $serverLog;

        if ( $this->connect ()->authenticate () ) {
            return true;
        }
    }

    public function isConnected () {
        return ( boolean ) $this->conn;
    }

    public function __get ( $name ) {
        return $this->$name;
    }

    public function connect () {
        $this->logAction ( "Connecting to {$this->Host}" );
        if ( $this->conn = ssh2_connect ( $this->Host, $this->port ) ) {
            return $this;
        }
        $this->logAction ( "Connection to {$this->Host} failed" );
        throw new Exception ( "Unable to connect to {$this->Host}" );
    }

    public function authenticate () {
        $this->logAction ( "Authenticating to {$this->Host}" );
        if ( ssh2_auth_password ( $this->conn, $this->user, $this->pass ) ) {
            return $this;
        }
        $this->logAction ( "Authentication to {$this->Host} failed" );
        throw new Exception ( "Unable to authenticate to {$this->Host}" );
    }

    public function sendFile ( $localFile, $remoteFile, $permision = 0644 ) {
        if ( ! is_file ( $localFile ) ) throw new Exception ( "Local file {$localFile} does not exist" );
        $this->logAction ( "Sending file $localFile as $remoteFile" );

        $sftp = ssh2_sftp ( $this->conn );
        $sftpStream = @fopen ( 'ssh2.sftp://' . $sftp . $remoteFile, 'w' );
        if ( ! $sftpStream ) {
            //  if 1 method failes try the other one
            if ( ! @ssh2_scp_send ( $this->conn, $localFile, $remoteFile, $permision ) ) {
                throw new Exception ( "Could not open remote file: $remoteFile" );
            }
            else {
                return true;
            }
        }

        $data_to_send = @file_get_contents ( $localFile );

        if ( @fwrite ( $sftpStream, $data_to_send ) === false ) {
            throw new Exception ( "Could not send data from file: $localFile." );
        }

        fclose ( $sftpStream );

        $this->logAction ( "Sending file $localFile as $remoteFile succeeded" );
        return true;
    }

    public function getFile ( $remoteFile, $localFile ) {
        $this->logAction ( "Receiving file $remoteFile as $localFile" );
        if ( ssh2_scp_recv ( $this->conn, $remoteFile, $localFile ) ) {
            return true;
        }
        $this->logAction ( "Receiving file $remoteFile as $localFile failed" );
        throw new Exception ( "Unable to get file to {$remoteFile}" );
    }

    public function cmd ( $cmd, $returnOutput = false ) {
        $this->logAction ( "Executing command $cmd" );
        $this->stream = ssh2_exec ( $this->conn, $cmd );

        if ( FALSE === $this->stream ) {
            $this->logAction ( "Unable to execute command $cmd" );
            throw new Exception ( "Unable to execute command '$cmd'" );
        }
        $this->logAction ( "$cmd was executed" );

        stream_set_blocking ( $this->stream, true );
        stream_set_timeout ( $this->stream, $this->stream_timeout );
        $this->lastLog = stream_get_contents ( $this->stream );

        $this->logAction ( "$cmd output: {$this->lastLog}" );
        fclose ( $this->stream );
        $this->log .= $this->lastLog . "\n";
        return ( $returnOutput ) ? $this->lastLog : $this;
    }

    public function shellCmd ( $cmds = array () ) {
        $this->logAction ( "Openning ssh2 Shell" );
        $this->shellStream = ssh2_Shell ( $this->conn );

        sleep ( 1 );
        $out = '';
        while ( $line = fgets ( $this->shellStream ) ) {
            $out .= $line;
        }

        $this->logAction ( "ssh2 Shell output: $out" );

        foreach ( $cmds as $cmd ) {
            $out = '';
            $this->logAction ( "Writing ssh2 Shell command: $cmd" );
            fwrite ( $this->shellStream, "$cmd" . PHP_EOL );
            sleep ( 1 );
            while ( $line = fgets ( $this->shellStream ) ) {
                $out .= $line;
                sleep ( 1 );
            }
            $this->logAction ( "ssh2 Shell command $cmd output: $out" );
        }

        $this->logAction ( "Closing Shell stream" );
        fclose ( $this->shellStream );
    }

    public function getLastOutput () {
        return $this->lastLog;
    }

    public function getOutput () {
        return $this->log;
    }

    public function disconnect () {
        $this->logAction ( "Disconnecting from {$this->Host}" );
        // if disconnect function is available call it..
        if ( function_exists ( 'ssh2_disconnect' ) ) {
            ssh2_disconnect ( $this->conn );
        }
        else { // if no disconnect func is available, close conn, unset var
            @fclose ( $this->conn );
            $this->conn = false;
        }
        // return null always
        return NULL;
    }

    public function fileExists ( $path ) {
        $output = $this->cmd ( "[ -f $path ] && echo 1 || echo 0", true );
        return ( bool ) trim ( $output );
    }
}