Knowledge base

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

Using SSH in PHP with phpseclib

Update 08/04/2026

This guide explains how to work without the PECL SSH2 client module, which is not available on Infomaniak Web Hosting and Cloud Servers, by using the phpseclib library instead, which works in native PHP without requiring a specific extension.

 

Introduction

  • Using the PECL SSH2 client results in errors such as No compatible key exchange algorithms found or Unable to exchange encryption keys in its latest available version.
  • Phpseclib provides:
    • 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('user', $key)) {
    exit('Authentication Failed');
}

echo $ssh->exec('ls -la');

Has this FAQ been helpful?