cesargb / ssh2-client
A PHP SSH2 client wrapper providing a clean and modern interface to the ssh2 extension.
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/cesargb/ssh2-client
Requires
- php: ^8.2
- ext-ssh2: *
Requires (Dev)
- laravel/pint: ^1.25
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5 || ^12.0
This package is auto-updated.
Last update: 2025-10-13 16:10:21 UTC
README
A PHP SSH2 client wrapper providing a clean and modern interface to the ssh2 extension.
Basic Usage
require 'vendor/autoload.php'; use Cesargb\Ssh\Ssh2Client; $sshClient = Ssh2Client::connect(host: 'localhost'); $fingerprint = $sshClient->fingerPrint(); echo "Server Fingerprint: {$fingerprint}\n"; $sshClient->withAuthPassword('username', 'password'); $commandResult = $sshClient->exec('ls -la'); $sshClient->disconnect(); if (! $commandResult->success()) { echo "Error Output: {$commandResult->errorOutput}\n"; exit($commandResult->getExitStatus()); } echo "Command Output: {$commandResult->output}\n";
Installation
composer require cesargb/ssh2-client
Authentication Methods
Password Authentication
$sshClient = Ssh2Client::connect(host: 'example.com', port: 22); $sshClient->withAuthPassword( username: 'root', password: 'root_password' );
Public Key Authentication with Passphrase
$sshClient = Ssh2Client::connect(host: 'example.com', port: 22); $sshClient->withAuthPublicKey( username: 'root', publicKey: '/path/to/public/key.pub', privateKey: '/path/to/private/key', passphrase: 'passphrase if required' );
Agent-Based Authentication
$sshClient = Ssh2Client::connect(host: 'example.com', port: 22); $sshClient->withAuthAgent('username');
Executing Commands
$sshClient = Ssh2Client::connect(host: 'example.com', port: 22) ->withAuthPassword('username', 'password'); $commandResult = $sshClient->exec('ls -l'); $sshClient->disconnect(); // $commandResult->succeeded() returns true if the command executed successfully // $commandResult->getExitStatus() returns the exit status of the command // $commandResult->output contains the command output by stdout // $commandResult->errorOutput contains any error output by stderr
SCP File Transfers
$sshClient = Ssh2Client::connect(host: 'example.com', port: 22) ->withAuthPassword('username', 'password'); $scpResult = $sshClient ->scpLocal('/local/path/to/file.txt') ->to('/remote/path/to/file.txt'); $sshClient->disconnect(); if (! $scpResult) { echo "File transfer failed.\n"; exit(1); } echo "File transferred successfully.\n";
Testing
composer test