简体   繁体   中英

PHP and phpseclib connection issue

I'm having a connection issue using PHP and phpseclib. I'm hoping that someone has seen this issue before. I've tested the SFTP connection successfully using Filezilla (local PC). I'm using somefile.pem file for the private RSA key. I can view the file and confirm that it says '-----BEGIN RSA PRIVATE KEY-----' at the beginning. Filezilla doesn't require a password, only the private key.

In the code below, I have the basic SFTP setup. I've tried all three ways to logging in. None seem to connect - connection error.

http://phpseclib.sourceforge.net/new/sftp/examples.html

Same .pem file is on the server trying to run the SFTP connection via phpseclib. I can var_dump and confirm the $pem variable is a string. When I var_dump the $key object, I don't see the RSA key text. I assumed it would be in the var_dump somewhere. It seems like the loadKey method is not loading the $pem string.

I also can not get the log output using echo $sftp->getLog();

define('NET_SFTP_LOGGING', 2); //turn on logging.

require_once('Net/SSH2.php');
require_once('Net/SFTP.php');
require_once('Net/SFTP/Stream.php');
require_once('Crypt/RSA.php');
require_once('Math/BigInteger.php');

$sftp = new Net_SFTP('***IP_ADDRESS***');
$key = new Crypt_RSA();
$key->setPassword('***PASSWORD***');
$pem = file_get_contents(FCPATH . '**PATH***/somefile.pem'); // RSA PRIVATE KEY
$key->loadKey($pem);

if (!$sftp->login('**USERNAME***', $key) && !$sftp->login('**USERNAME***', '**PASSWORD***')) {
//if (!$sftp->login('**USERNAME***', '**PASSWORD***')) {
//if (!$sftp->login('**USERNAME***', $key)) {
    echo "<pre>";
    var_dump($pem);
    var_dump($key);
    var_dump($sftp);
    echo $sftp->getSFTPLog();
    exit('Login Failed');
}

Error notice:

A PHP Error was encountered
Severity: User Notice
Message: Cannot connect to **IP**:22. Error 110. Connection timed out
Filename: Net/SSH2.php
Line Number: 960

PHP Info shows the following:

Registered PHP Streams: https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip

Registered Stream Socket Transports: tcp, udp, unix, udg, ssl, sslv3, sslv2, tls

It errors way before any private key is used. So it's not related to the key.

Check the Net\\SSH2.php around line 960:

$this->fsock = @fsockopen($this->host, $this->port, $errno, $errstr, $timeout);
if (!$this->fsock) {
    user_error(rtrim("Cannot connect to $host. Error $errno. $errstr"));
    return false;
}

Make sure there's a connectivity from your web server to the SFTP server. Your webserver may not allow outbound connections at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM