PHP code example of divineomega / php-ssh-connection

1. Go to this page and download the library: Download divineomega/php-ssh-connection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

divineomega / php-ssh-connection example snippets


$connection = (new SSHConnection())
            ->to('test.rebex.net')
            ->onPort(22)
            ->as('demo')
            ->withPassword('password')
         // ->withPrivateKey($privateKeyPath)
         // ->withPrivateKeyString($privateKeyContents)
         // ->withPrivateKey($encryptedPrivateKeyPath, $passphrase)
         // ->withExpectedFingerprint('SHA256:base64-fingerprint-from-a-trusted-source')
         // ->timeout(30)
            ->connect();

$command = $connection->run('echo "Hello world!"');

$command->getOutput();  // 'Hello world!'
$command->getError();   // ''
$command->getExitStatus(); // 0
$command->isSuccessful();  // true
$command->hasTimedOut();   // false

$connection->upload($localPath, $remotePath); // SFTP
$connection->download($remotePath, $localPath); // supports recursive directory downloads

$connection->runCommands([
    'cd /var/www/html',
    'mkdir -p app',
    'cd app',
    'touch index.php',
]);

$connection = (new SSHConnection())
    ->to('example.com')
    ->as('username')
    ->withPrivateKey('/home/user/.ssh/id_rsa')
    ->withExpectedFingerprint('SHA256:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU')
    ->connect();

$md5Fingerprint    = $connection->fingerprint(SSHConnection::FINGERPRINT_MD5); // colon-separated hex
$sha1Fingerprint   = $connection->fingerprint(SSHConnection::FINGERPRINT_SHA1);
$sha256Fingerprint = $connection->fingerprint(SSHConnection::FINGERPRINT_SHA256); // OpenSSH base64
$sha512Fingerprint = $connection->fingerprint(SSHConnection::FINGERPRINT_SHA512);
$openSshPublicKey  = $connection->hostPublicKey();

$connection->withPrivateKey('/secure/id_rsa', 'key passphrase');
$connection->withPrivateKeyString($privateKeyContents, 'key passphrase');