Tiny SSH package that allows you to execute commands over SSH connections. It supports both password and private key authentication and is built on the phpseclib library.
Install the package via composer:
composer require sandeshjangam/tiny-ssh-php
Simple SSH command using password authentication:
$ssh = (new Ssh)
->host($ip)
->port(22)
->user($user)
->password($password)
// ->privateKey($privateKey)
// ->privateKeyPath($privateKeyPath)
->connect();
$response = $ssh->execute('whoami');
$response->getOutput(); // 'username'
$response->getError(); // ''
Get the output:
$response->getOutput(); // It returns the `stdout`
Get the error if any:
$response->getError(); // It returns the `stderr`
Get the exit status:
$response->getExitStatus(); // 0 for success. To check if the command ran successfully
To execute multiple commands pass an array of commands:
$response = $ssh->execute(['whoami', 'ls -la']);
Or pass the commands as a string separated by &&
:
$response = $ssh->execute('whoami && ls -la');
You can set the timeout
. Default is 10 seconds:
->timeout(60) // 60 seconds
You can use Private Key
content:
->privateKey('private_key_content')
Or Private Key
file path:
->privateKeyPath('/home/user/.ssh/id_rsa')
To upload or download files and directories, you'll need to use the SFTP class:
$sftp = (new Sftp)
->host($ip)
->port(22)
->user($user)
->password($password)
// ->privateKey($privateKey)
// ->privateKeyPath($privateKeyPath)
->connect();
To upload
a file or directory to the remote server:
$sftp->upload('local/file/path', 'remote/file/path')
To download
a file or directory from the remote server:
$sftp->download('remote/file/path', 'local/file/path')
To disconnect the SSH connection:
$ssh->disconnect();
To disconnect the SFTP connection:
$sftp->disconnect();
composer test
The MIT License (MIT).