0

I am trying to execute the shell script on the remote server in Java. For SSH connectivity in Java, I referred this link: Run a command over SSH with JSch. But it talks about providing the username, password information. In my case, I want to provide the public key file. Is there any way to do the same?

Swapnil
  • 801
  • 3
  • 19
  • 42

1 Answers1

0

Try something like this:

jsch.addIdentity(privateKeyPath);        
session = jsch.getSession(username, host, port);
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");

You can convert ppk to pem and vice-versa using Putty keygen tool if you're on Windows. I am sure there would a similar utility for Linux.

sid
  • 83
  • 8
  • 1
    You don't need keyboard or password authentications if you're sending a public key over... – Makoto Nov 12 '18 at 22:59
  • @Makoto: So do you mean if I have the .pem file, do I need to ignore the second parameter of `session.setConfig()`? – Swapnil Nov 12 '18 at 23:01
  • @Swaps: A .pem isn't a public key. You'll have to convert that first. – Makoto Nov 12 '18 at 23:03
  • I think that was for passphrases. But, you can ignore it. – sid Nov 12 '18 at 23:03
  • This answer shows exactly what the accepted answer to duplicate questions shows. Please do not duplicate information. Vote for this question to be closed as a duplicate instead. – Martin Prikryl Nov 13 '18 at 07:41