Question

I need to use FTP(S) and SFTP in my extension to receive and send files. I got FTP(S) to work with no problems whatsoever (using Varien_Io_Ftp), but I'm struggling with SFTP.

The problem is that there is an error that occurs only sporadically. Sometimes everything works as expected and the file is transferred, but sometimes it just refuses to connect to the server at all (Error: Unable to open SFTP connection as user@localhost).

I have traced it down to the file lib/phpseclib/Net/SSH2.php in line 1274 (getting response to 'ssh-userauth' packet), which returns false.

I'm fairly certain that the problem is not the SSH/SFTP server, because I can connect via CLI (using both ssh and sftp) every time.

Now what can I do to make this work reliably? I don't think updating the phpseclib is an option since it's part of / shipped with Magento core, right? I'm using Magento 1.9.3.2 by the way.

Edit: Here is what I do with the SFTP class, if that helps:

$this->connection = new Varien_Io_Sftp();
$args = array(
    'host' => $host,
    'username' => $user,
    'password' => $password,
);
$this->connection->open($args);
Was it helpful?

Solution

After a lot of trial and error, I was able to solve the problem. There seems to be an issue with the phpseclib version shipped with Magento.

Here is my solution for future reference:

I added the current version (1.0.6) of phpseclib as a custom library (I put it into the directory magento/lib/phpseclib_106/). Then I could build my own wrapper class for SFTP, so I don't use Varien_Io_Sftp anymore.

In the wrapper class I used

set_include_path(get_include_path() . PATH_SEPARATOR . Mage::getBaseDir('lib') . '/phpseclib_106');
require_once('Net/SFTP.php');

Now the connection works every single time.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top