Knowledge base

1000 FAQs, 500 tutorials and instructional videos. Here, there are only solutions!

Using 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. This library works with native PHP without requiring any specific extension.

 

Preamble

  • The use of PECL SSH2 client results in errors of type No compatible key exchange algorithms found or Unable to exchange encryption keys in its latest available version.
  • Phpseclib allows:
    • 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');

Has this FAQ been helpful?