0

I am not able to connect to Sftp in magento2.2.5

Here is my code:

public function execute()
    {
        $connection = $this->sftp->open(
                    array(
                        'host' => 'myhostname',
                        'username' => 'myusername',
                        'password' => 'mypassword',
                        'port' =>22,
                        'passive' => true
                    )
                );
        if($connection){
        echo "true";

        }else{
            echo "false";
        }
                //print_r($connection);
        die;

But it is returning false.

I don't know what is the problem.

Any help would be appreciated.

  • remove if and else and check by var_dump($this->sftp->ls()); – Prathap Gunasekaran May 24 '19 at 11:23
  • The connection is being listed.. But the connection is failed. – Amy May 24 '19 at 11:32
  • Are you working on cloud magento ? – Chirag Patel May 24 '19 at 11:52
  • Yes, I am working on cloud.. – Amy May 24 '19 at 12:07
1

In Magento, $this->sftp->open(); returns null. you can php function is_null() function in the condition.

public function execute()
    {
        $connection = $this->sftp->open(
                    array(
                        'host' => 'myhostname',
                        'username' => 'myusername',
                        'password' => 'mypassword',
                        'port' =>22,
                        'passive' => true
                    )
                );
        if(is_null($connection)){
        echo "true";
         //Here your code.....

        }else{
            echo "false";
        }
| improve this answer | |
0

Kindly install PHP ssh2 extension and then use the following code.

If you get not connected double check your IP is whitelisted or not

$strServer = '*******';
$strServerPort = '*******';
$strServerUsername = '*******';
$strServerPassword = '*******';

//connect to server
$resConnection = ssh2_connect($strServer, $strServerPort);

if(ssh2_auth_password($resConnection, $strServerUsername, $strServerPassword))
{
     //Initialize SFTP subsystem            
     echo "connected";
}
else
{
      echo "not connected";
} 

It works for me.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.