The PHP SSH Connection package provides an elegant syntax to connect to SSH servers and execute commands. It supports password and public-private key authentication, and can capture command output and errors.
Supported runtimes: PHP 7.2+ and PHP 8.x.
Install with Composer:
composer require jord-jd/php-ssh-connection$connection = (new SSHConnection())
->to('test.rebex.net')
->onPort(22)
->as('demo')
->withPassword('password')
// ->withPrivateKey($privateKeyPath)
// ->withPrivateKeyString($privateKeyContents)
// ->timeout(30)
->connect();
$command = $connection->run('echo "Hello world!"');
$command->getOutput(); // 'Hello world!'
$command->getError(); // ''
$connection->upload($localPath, $remotePath);
$connection->download($remotePath, $localPath); // supports recursive directory downloadsEach run() call executes in a fresh shell context. If you need stateful command execution (for example cd then touch), use runCommands():
$connection->runCommands([
'cd /var/www/html',
'mkdir -p app',
'cd app',
'touch index.php',
]);For security, you can fingerprint the remote server and verify it remains the same across connections.
$fingerprint = $connection->fingerprint(); // defaults to MD5 for backward compatibility
if ($newConnection->fingerprint() !== $fingerprint) {
throw new Exception('Fingerprint does not match!');
}Available fingerprint types:
$md5Fingerprint = $connection->fingerprint(SSHConnection::FINGERPRINT_MD5);
$sha1Fingerprint = $connection->fingerprint(SSHConnection::FINGERPRINT_SHA1);
$sha256Fingerprint = $connection->fingerprint(SSHConnection::FINGERPRINT_SHA256);
$sha512Fingerprint = $connection->fingerprint(SSHConnection::FINGERPRINT_SHA512);The package test suite includes SSH integration tests. Set these variables before running tests:
RUN_SSH_INTEGRATION_TESTS=1SSH_TEST_HOSTSSH_TEST_PORTSSH_TEST_USERSSH_TEST_PRIVATE_KEY_PATHorSSH_TEST_PRIVATE_KEY_CONTENTSSSH_TEST_PASSWORD(only required for password-auth test)
Then run:
vendor/bin/phpunit