Reputation: 647
I have a pretty simple PHP script that tries to connect to SFTP server from a CENTOS 7 box. Using phpseclib via composer.
<?php
require('vendor/autoload.php');
$usr = 'xx';
$pwd = 'xx';
$host = 'sftp.domain.com.au';
$sftp = new SFTP($host);
if($sftp->login($usr, $pwd)){
echo "Connected.";
}else {
echo "Failed";
}
?>
And even used an RSA key
<?php
require('vendor/autoload.php');
$usr = 'xx';
$pwd = 'xx';
$host = 'sftp.domain.com.au';
$sftp = new SFTP($host);
$rsa = new RSA();
$rsa->loadKey(file_get_contents("../../.ssh/id_rsa"));
if($sftp->login($usr, $pwd, $rsa)){
echo "Connected.";
}else {
echo "Failed";
}
?>
The script above with RSA actually did work a few times and got me connected. So I thought I need to understand why it works, and change a few things. I tried generating another key overwriting the existing key. But this time it didnt work anymore.
I also turned on the NET_SFTP_LOGGING with no display error just a plain "Connection closed by server". I have been working on this for days..
Only thing I could think of is when I regenerated the key and overwrite the ID_RSA somehow maybe the server thinks its not the right key anymore?
Also when I connect using WinSCP, it seems the FTP server is giving me an algorithm: ssh-rsa 4096 , sha256 and md5 code. Is this something I need to use in my PHP script to properly connect? Just trying to rule out any possibilities.
MORE DETAILS:
Please anyone give us some advise as this is consuming me for DAYS hopefully not for weeks! Thanks in advance.
Upvotes: 0
Views: 2100
Reputation: 16792
From your code:
$rsa->loadKey("../../.ssh/id_rsa");
Try this:
$rsa->loadKey(file_get_contents("../../.ssh/id_rsa"));
Also,
if($sftp->login($usr, $pwd, $rsa)){
Are you actually wanting to use multi-factor auth? In my experience SSH servers setup to do that are quite rare. What seems more likely is that you have a password protected RSA private key, at which point you'd do something more like this:
$rsa->setPassword($pwd);
$rsa->loadKey(file_get_contents("../../.ssh/id_rsa"));
Upvotes: 1