Question

I'm trying to connect to a SSH2 server through SFTP in PHP using PHPSecLib. But my problem is that, the server requires a key file that is password protected but also username and password. It seems like that it's only possible to provide either username + password or username + keyfile (optionally password protected).

How can I sign on using all these things?

http://phpseclib.sourceforge.net/

My code looks as

$key = new Crypt_RSA();
$key->setPassword('keypwd');
$key->loadKey(file_get_contents('ssh_file.ppk'));

$sftp = new Net_SFTP('hostname', 22);

if (!$sftp->login('username', $key))
  echo 'Failed';
else
  echo 'Success';

But where should I put 'password'?

-- Edit: The server only requires the RSA key, but somehow the key is not loaded properly

Was it helpful?

Solution

There is an example specifying how to load a password protected key file on the phpseclib website. Note the line $key->setPassword('whatever');

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

$ssh = new Net_SSH2('www.domain.tld');
$key = new Crypt_RSA();
$key->setPassword('whatever');
$key->loadKey(file_get_contents('privatekey'));
if (!$ssh->login('username', $key)) {
    exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');

Keep in mind you're not sending the password to the server. You just need the password in order to successfully load the key file. Then the username and key are sent to the server to authenticate you.

OTHER TIPS

Unsolvable since the script requires RSA and the key is DSA

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top