Can someone give me an example of using SSHJ for Public Key Authentication? I realise this question is essentially identical to ssh example of private/public key authentication, however the answer by the author https://stackoverflow.com/users/126346/shikhar refers to a google user group that no longer exists, and I am having trouble getting it to work.
Thanks! Phil
To generate an SSH key pair, run the command ssh-keygen. It will look like this when you run it: laptop1:~ yourname$ ssh-keygen Generating public/private rsa key pair. You'll be prompted to choose the location to store the keys.
An SSH2 public key in OpenSSH format will start with "ssh-rsa". The idea behind all of this is that once you have keys on the remote server and your local host, access will be simpler since the server will only grant access to someone who has the matching private key.
To generate an SSH private/public key pair for your use, you can use the ssh-keygen command-line utility. You can run the ssh-keygen command from the command line to generate an SSH private/public key pair. If you are using Windows, by default you may not have access to the ssh-keygen command.
We built the overthere framework on top of SSHJ. Which can connect also connect using key files. The following piece of code should work, but make sure you add the bouncycastle library to your classpath.
SSHClient client = new SSHClient();
String username = "johndoe";
File privateKey = new File("~/.ssh/id_rsa");
KeyProvider keys = client.loadKeys(privateKey.getPath());
client.authPublickey(username, keys);
Hope that helps.
I just had this issue as well. I ended up changing
client.authPublickey(user, "id_rsa.pub")
to
client.authPublickey(user, client.loadKeys("id_rsa"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With