Here is my code:
error_reporting(E_ALL);
require __DIR__ . '/vendor/autoload.php';
use phpseclib\Net\SSH2;
use phpseclib\Crypt\RSA;
$ssh = new SSH2('stg.net');
$key = new RSA();
$key->loadKey(file_get_contents('/Users/me/.ssh/my_private_key'));
if (!$ssh->login('username', $key)) {
print_r($ssh->getLastError());
print_r($ssh->getErrors());
exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
Output:
Array
(
)
In vendor/phpseclib/phpseclib/phpseclib/Net/SSH2.php there is function _privatekey_login($username, $privatekey)
$publickey = $privatekey->getPublicKey(RSA::PUBLIC_FORMAT_RAW);
if ($publickey === false) {
return false;
}
I'm getting false, maybe I have to set public key too? How can it be done?
How to debug it?
+++ UPDATE +++
I tried advices/hints from these tickets too:
define('NET_SSH2_LOGGING', 2);at the top and then$ssh->getLog()before theexit;. That said, based on your_privatekey_logincomment it sounds like maybe you're trying to use a DSA or ECDSA key in place of an RSA key. Most RSA private keys include the public key within them and it's hard to imagine you using one that doesn't. I mean, it's not impossible but it is improbable.