0

I am having trouble connecting to an SFTP server using a .ppk file that are provided.

I have tried this ppk file in Filezilla and it works. According to one of the comments in another question it is better to use phpseclib but i didnt find any instructions on how to download files from SFTP using a ppk file. Any directions or suggestions?

Here is the code I'm not sure if that will help or not.

include('Net/SSH2.php');
include('Crypt/RSA.php');
include('Net/SFTP.php');


DEFINE('SERVER','sample.sftp.com');
DEFINE('USER','sampleUserName');
DEFINE('KEY','sample_key_22733_priv.ppk');

$sftp = new Net_SFTP(SERVER);

//I guess this password is useless here
//and i will have to use my ppk file here but i don't know how

if (!$sftp->login(USER, 'password')) {
    exit('Login Failed');
}

var_dump($sftp->nlist());
Community
  • 1
  • 1
A Web-Developer
  • 868
  • 8
  • 29
  • check this question [http://stackoverflow.com/questions/16132200/php-bash-creating-ppk-out-of-openssh-key-with-passphrase](http://stackoverflow.com/questions/16132200/php-bash-creating-ppk-out-of-openssh-key-with-passphrase) – mdamia Aug 24 '15 at 01:12
  • @mdamia I dont get it. It seems all the instructions stopped after getting the private key. What do i do from there? – A Web-Developer Aug 24 '15 at 01:25

2 Answers2

3

phpseclib works perfectly fine with PPK files, assuming they're RSA keys and not DSA keys. Just initialize a Crypt_RSA object and then call loadKey(file_get_contents('path/to/key.ppk')) on that object.

To download files do $sftp->get('/path/to/filename.ext').

Here's your orig code modified to include all this:

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


DEFINE('SERVER','sample.sftp.com');
DEFINE('USER','sampleUserName');
DEFINE('KEY','sample_key_22733_priv.ppk');

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents(KEY));

$sftp = new Net_SFTP(SERVER);

//I guess this password is useless here
//and i will have to use my ppk file here but i don't know how

if (!$sftp->login(USER, $rsa)) {
    exit('Login Failed');
}

echo $sftp->get('/path/to/filename.ext');
neubert
  • 15,947
  • 24
  • 120
  • 212
1

Update: As @neubert confirmed, PPK files are supported but you do need to make sure they are RSA as I explained, but you do not need them in OpenSSH format


Convert your PPK to an OpenSSH RSA key. You can convert this using the PuTTYgen executable: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

Once you have it in an RSA key, you can use this with phpseclib: http://phpseclib.sourceforge.net/ssh/auth.html#rsakey

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95