I've recently tried to implement the phpseclib library to log into a SFTP server to list all the files in a directory and download them. After downloading them, move them to a different directory on the server.
I'm not having much luck... So following the code straight from their site, but added logging.
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
require('Net\SSH2.php');
require('Crypt\RSA.php');
define('NET_SSH2_LOGGING', 2);
$ssh = new Net_SSH2('host_name', 'port');
$key = new Crypt_RSA();
$key->loadKey(file_get_contents('path_to_rsa_file'));
if (!$ssh->login('username', $key)) {
exit('Login Failed');
}
$ssh->exec("pwd");
That results in the following error:
Unable to request pseudo-terminal
Admittedly this is an older version, I'm not sure exactly which one, so I thought maybe I should update. After pulling in the latest version via composer, and running the same code as above, I get no errors, but I get this in the logs:
<- NET_SSH2_MSG_USERAUTH_SUCCESS (since last: 0.1395, network: 0.1394s)
-> NET_SSH2_MSG_CHANNEL_OPEN (since last: 0.0006, network: 0.0005s)
00000000 00:00:00:07:73:65:73:73:69:6f:6e:00:00:00:00:7f ....session.....
00000010 ff:ff:ff:00:00:40:00 .....@.
<- NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION (since last: 0.1232, network: 0.1231s)
00000000 00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:80:00 ................
-> NET_SSH2_MSG_CHANNEL_REQUEST (since last: 0.0008, network: 0.0006s)
00000000 00:00:00:00:00:00:00:04:65:78:65:63:01:00:00:00 ........exec....
00000010 03:70:77:64 .pwd
<- NET_SSH2_MSG_CHANNEL_FAILURE (since last: 0.123, network: 0.1228s)
00000000 00:00:00:00 ....
Any one have any ideas? I've also tried using the SFTP class, but it failed with other problems "Notice: Connection closed prematurely".
Thanks for any help!
Looks like the server just doesn't support
exec()
. You might have better luck withread()
/write()
:http://phpseclib.sourceforge.net/ssh/examples.html#interactive
As for SFTP... I'd need to see logs to have any ideas.