Laravel Unable to connect my SFTP server with privatekey.pem / privatekey.ppk

59 Views Asked by At

im trying to connect my SFTP Server with privatekey.pem / privatekey.ppk But still getting same error which is "Could not login with username : XXXXX , host : XX.XX.XX.XX" [![enter image description here][1]][1] [1]: https://i.stack.imgur.com/icMPx.png

Initially, i have privatekey.ppk then i convert to privatekey.pem using puttygen because i thought that Laravel cant use file .ppk . But the result still same.

btw , I can access through WinSCP with my privatekey.ppk and Terminal with my privatekey.pem . But unfortunately Laravel , i cannot get through.

Some info im using :

  • Laravel 8
  • Php 7
  • league/flysystem-sftp: 1.1
  • phpseclib/phpseclib: 2.0

I also have try phpseclib connection but still same

use phpseclib\Crypt\RSA;
use phpseclib\Net\SFTP;

    $key = new RSA;
    $key->loadKey(file_get_contents('C:/key/privatekey.pem'));
    
    $sftp = new SFTP('XX.XX.XX.XX');
    
    if (!$sftp->login('XXXXX', $key)) {
        dd($sftp->getLog());
    }

my config\filesystems.php

        'sftp' => [
        'driver' => 'sftp',
        'host' => 'XX.XX.XX.XX',
        'username' => 'XXXX',
        'privateKey' => file_get_contents('C:/key/privatekey.pem'),
        'root' => '/upload'
    ]

my myprivatekey.pem

-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACAiSbn2URP33h/UjsByVXQA1H/sb8eNTf4PjPFlRdYp4gAAAJj7Xoxl+16M
ZQAAAAtzc2gtZWQyNTUxOQAAACAiSbn2URP33h/UjsByVXQA1H/sb8eNTf4PjPFlRdYp4g
-----END OPENSSH PRIVATE KEY-----

Hope somebody can give me some idea what's going on. thank you

1

There are 1 best solutions below

0
Erik Roznbeker On

Maybe problem is that privateKey in config should be path to file, not content.

I'm using SFTP in my app:

I followed this docs: https://laravel.com/docs/10.x/filesystem#sftp-driver-configuration

.env

 SFTP_HOST=xxx.xxx.xxx.xxx
 SFTP_USERNAME=foo
 SFTP_PASSWORD=bar
 SFTP_PORT=22
 SFTP_KEY=/private/key/file/id_rsa
 SFTP_PATH=/folder/on/remote/server

filesystems.php

'remote_sftp' => [
        'driver' => 'sftp',
        'host' => env('SFTP_HOST'),
        'username' => env('SFTP_USERNAME'),
        'password' => env('SFTP_PASSWORD'),
        'port' => (int)env('SFTP_PORT', 22),
        'privateKey' => env('SFTP_KEY'),
        'passphrase' => '',
        'path' => env('SFTP_PATH'),
    ],

and i get files on that remote disk:

$sftpStorage = Storage::disk('remote_sftp');
$remoteFiles = $sftpStorage->allFiles(config('filesystems.disks.remote_sftp.path'));

I hope that this can help.