Magento 2 SSH Library

One of the composer dependcies in Magento 2 is phpseclib. This means for integrations using ssh you can use this instad of needing the php ssh module for your integration you can use this module.

To run a remote ssh command you can use the following snippet as an example for either the straight echo or the callback way of dealing with the result:

use phpseclib\Net\SSH2;

$ssh = new SSH2('my-ssh-server.com');
$ssh->login($username, $password);

if($ssh->isConnected()) {
	echo $ssh->exec('ls -la');
	$ssh->exec('ls -la' function($result){
		echo $result
	});
}

To do a file transfer use the following

use phpseclib\Net\SFTP;

$sftp = new SFTP('my-ssh-server.com');
$sftp->login($username, $password);

if($sftp->isConnected()) {
	echo $sftp->get('file1.txt');
}

You can also use the php streams with the library by running the following

use phpseclib\Net\SFTP\Stream;

Stream::register()
$txt = fopen('sftp://user:pass@host.com:/home/user/file.txt');

Tags: Magento2sshphpseclib