Knowledge base
1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Use SSH in PHP with phpseclib
This guide explains how to work without the PECL SSH2 client
module, which is unavailable on Infomaniak Web Hosting and Cloud Servers, by using the phpseclib
library instead, which runs in native PHP without requiring any specific extension.
Foreword
- Using
PECL SSH2 client
results in errors such asNo compatible key exchange algorithms found
orUnable to exchange encryption keys
in its latest available version. Phpseclib
allows for:- SSH authentication via password or private key.
- Remote command execution.
- Secure file transfer (SFTP).
- SSH key management.
Using phpseclib
To integrate an SSH connection into a PHP script, use phpseclib
as follows:
use phpseclib3\Net\SSH2;
use phpseclib3\Crypt\PublicKeyLoader;
$ssh = new SSH2('domain.xyz');
$key = PublicKeyLoader::load(file_get_contents('/path/to/private_key'));
if (!$ssh->login('utilisateur', $key)) {
exit('Authentication Failed');
}
echo $ssh->exec('ls -la');
Link to this FAQ: