php - Connecting via SSH with RSA using PHPSECLIB -


i learning how use 'phpseclib' library.

i have managed connect via ssh using username/password combo.

i want connect using private key, can't seem able it. let me explain how have gone , can point me errors of 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 can see, set public key format 'openssh' , create key. using values created createkey method put key in file called 'privatekey' (no extension, unsure if needed it's text). normal, loading key , using value in login.

i've used official docs (which aren't great) , google'd around , i've not had luck either.

the output of getlasterror is:

ssh_msg_userauth_failure: publickey,password

thanks in advance assistance.

you can create key pairs day on client side unless public part of key pair generated in /home/user/.ssh/authorized_keys file it's not going matter. , if you're creating keys , trying connect them in same script... it's unlikely they're going in file.

in particular case should load private key corresponding public key in /home/user/.ssh/authorized_keys file. load $rsa->loadkey(...) , /then/ try use rsa object in ssh's login method.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -