PHP code example of spatie / ssh

1. Go to this page and download the library: Download spatie/ssh 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/ */

    

spatie / ssh example snippets


Ssh::create('user', 'host')->execute('your favorite command');

$process = Ssh::create('user', 'example.com')->execute('your favorite command');

$process = Ssh::create('user', 'example.com')->executeAsync('your favorite command');

$process->isSuccessful();

$process->getOutput();

$process = Ssh::create('user', 'example.com')->execute([
   'first command',
   'second command',
]);

$port = 123;

Ssh::create('user', 'host', $port);

Ssh::create('user', 'host')->usePort($port);

Ssh::create('user', 'host')->setTimeout(100);

Ssh::create('user', 'host')->useJumpHost("$jumpuser@$jumphost:$jumpport");

Ssh::create('user', 'host')->useMultiplexing($controlPath, $controlPersist);

// Ssh::create('user', 'host')->useMultiplexing('/home/.ssh/control_masters/%C', '15m');


Ssh::create('user', 'host')->usePrivateKey('/home/user/.ssh/id_rsa');

Ssh::create('user', 'host')->disableStrictHostKeyChecking();

Ssh::create('user', 'host')->enableQuietMode();

Ssh::create('user', 'host')->disablePasswordAuthentication();

Ssh::create('user', 'host')->upload('path/to/local/file', 'path/to/host/file');

Ssh::create('user', 'host')->download('path/to/host/file', 'path/to/local/file');

Ssh::create('user', 'host')->configureProcess(fn (Process $process) => $process->setTimeout(null));

Ssh::create('user', 'host')->onOutput(function($type, $line) {echo $line;})->execute('whoami');

Ssh::create('user', 'host')->removeBash();