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 clientresults in errors such asNo compatible key exchange algorithms foundorUnable to exchange encryption keysin its latest available version. Phpsecliballows 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: