3

I have to connect to SFTP server by using PHP. I am using phpseclib for that. I found some example on Internet, but I am not able to connect to SFTP. I am using a custom port (2222) to connect to SFTP. Please tell me where I can define custom port to connect to SFTP.

<?php
include('Net/SFTP.php');

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}
?>

It is showing "Login Failed".

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Rupzz
  • 136
  • 1
  • 3
  • 16
  • Give us more information. See http://phpseclib.sourceforge.net/documentation/net.html#net_ssh_debug – Martin Prikryl Jan 30 '15 at 08:59
  • I am running the same code. when i connecting using filezilla i have to specify port 2222. But when i trying connecting using phpseclib by using code "Net_SFTP('www.domain.tld', 2222);" I am not able to connect. I changed the port in SFTP.php also. But still getting "Login Failed". – Rupzz Jan 30 '15 at 11:17
  • The link shows how to enable logging and retrieve the log. Please do that and include the log into your question. – Martin Prikryl Jan 30 '15 at 11:32
  • it show Array() Login Failed – Rupzz Jan 30 '15 at 11:34
  • Please include your new complete code, including the debug statements. – Martin Prikryl Jan 30 '15 at 11:45
  • login('username', 'password')) { exit('Login Failed'); } else{ echo "connected"; } echo $sftp->pwd() . "\r\n"; print_r($sftp->nlist()); print_r($sftp->getSFTPErrors()); echo $sftp->getLog(); ?> – Rupzz Jan 30 '15 at 11:51
  • Well, we need to see the log, when it fails. So can you move the `echo $sftp->getLog();` and `print_r($sftp->getSFTPErrors())` just before `exit('Login Failed');` ? I also do not understand where you got that "Array()" from, as you have no debugging code before the `exit`. Anyway, next time, please edit your question to include more information (see "edit" link below the question). So not post multi-line code/output to comments, it's unreadable. – Martin Prikryl Jan 30 '15 at 11:56
  • Apologize for last. I moved these two lines before "Login Failed". I am getting Array () Login Failed. – Rupzz Jan 30 '15 at 12:06
  • Why did you posted [the same question](http://stackoverflow.com/questions/28236469/phpseclib-showing-login-failed) again? – Martin Prikryl Jan 30 '15 at 16:10
  • Do `define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX)` - not `define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX)`. The latter only captures SFTP layer packets and SSH2 operates at a layer below SFTP. So to diagnose SSH2 issues it's SSH2 logs that we'd need to see. Also, `$sftp->getLog()` returns SSH2 logs. If you wanted SFTP logs you'd need to use `$sftp->getSFTPLog()`. – neubert Feb 01 '15 at 01:22

1 Answers1

16

You can define a custom port like so:

Net_SFTP('www.domain.tld', 2222);

You can see it when you look in the constructor from SFTP class which is like this:

function Net_SFTP($host, $port = 22, $timeout = 10)
{

}
Daan
  • 12,099
  • 6
  • 34
  • 51