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?
Asked
Active
Viewed 549 times
1 Answers
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
-
1You 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
-
-
-
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