I am learning how to use the 'phpseclib' library.
I have successfully managed to connect via SSH using the username/password combo.
I want to connect now using a private key, but I can't seem to be able to do it. Let me explain how I have gone about this and hopefully someone can point me to the errors of my way.
<?php
use phpseclib\Net\SSH2;
use phpseclib\Crypt\RSA;
include('vendor/autoload.php');
define('NET_SSH2_LOGGING', SSH2::LOG_COMPLEX);
$ssh = new SSH2('MYIP');
$rsa = new RSA();
$rsa->setPublicKeyFormat(RSA::PUBLIC_FORMAT_OPENSSH);
extract($rsa->createKey(2048));
file_put_contents('privatekey', $privatekey);
if($rsa->loadKey(file_get_contents('privatekey')))
{
echo('Key loaded');
} else {
throw new Exception('Key not loaded');
}
if($ssh->login('chris', $rsa))
{
echo('Connected');
} else {
echo $ssh->getLastError();
throw new Exception('Not Connected');
}
As you can see, I set the public key format to 'openssh' and then create the key. Using the values created by the createKey
method I put that key in a file called 'privatekey' (no extension, but unsure if needed as it's just text). Then the normal, loading the key and using it as a value in the login.
I've used the official docs (which aren't great) and Google'd around and I've not had much luck with either.
The output of the getLastError
is:
SSH_MSG_USERAUTH_FAILURE: publickey,password
Thanks in advance for any assistance.