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 module PECL SSH2 client
not available on Web hosting and Cloud Servers Infomaniak, using the library instead phpseclib
, which works in native PHP without requiring specific extension.
Preamble
- The use of
PECL SSH2 client
leads to type errorsNo compatible key exchange algorithms found
orUnable to exchange encryption keys
in its latest version available. Phpseclib
allows:- SSH authentication by password or private key.
- The execution of remote commands.
- Secure file transfer (SFTP).
- Management of SSH keys.
Use phpseclib
To integrate a 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: