La librairie phpseclibpermet de se connecter en SSH ou SFTP à une serveur pour exécuter des commandes ou transférer des fichiers depuis une application PHP.

Exemple d’utilisation:

SFTP

Connexion

use phpseclib3\Net\SFTP;
$sftp = new SFTP('localhost');
$sftp->login('username', 'password');

Upload

$sftp->put('filename.remote', 'filename.local', SFTP::SOURCE_LOCAL_FILE);

Download

// outputs the contents of filename.remote to the screen echo
$sftp->get('filename.remote');
// copies filename.remote to filename.local from the SFTP server
$sftp->get('filename.remote', 'filename.local');

SSH

Connexion

use phpseclib3\Net\SSH2;
 
$ssh = new SSH2('localhost', 22);
if ($expected != $ssh->getServerPublicHostKey()) {
	throw new \Exception('Host key verification failed');
}