Skip to content

Tiny SSH PHP package that allows you to execute commands over SSH connections. It supports both password and private key authentication.

License

Notifications You must be signed in to change notification settings

sandeshjangam/tiny-ssh-php

Repository files navigation

Tiny SSH PHP

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.

Installation

Install the package via composer:

composer require sandeshjangam/tiny-ssh-php

Usage

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();   // ''

Response of a command

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

Running multiple commands

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');

Use timeout for the long running command

You can set the timeout. Default is 10 seconds:

->timeout(60) // 60 seconds

Use private key as a string or file

You can use Private Key content:

->privateKey('private_key_content')

Or Private Key file path:

->privateKeyPath('/home/user/.ssh/id_rsa')

Upload or Download files and directories

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')

Disconnect SSH or SFTP connection

To disconnect the SSH connection:

$ssh->disconnect();

To disconnect the SFTP connection:

$sftp->disconnect();

Testing

composer test

Credits

License

The MIT License (MIT).

About

Tiny SSH PHP package that allows you to execute commands over SSH connections. It supports both password and private key authentication.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages