Can we use JSch for SSH key-based communication?

It is possible. Have a look at JSch.addIdentity(…) This allows you to use key either as byte array or to read it from file. import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; public class UserAuthPubKey { public static void main(String[] arg) { try { JSch jsch = new JSch(); String user = “tjill”; String host … Read more

Run a command over SSH with JSch

The following code example written in Java will allow you to execute any command on a foreign computer through SSH from within a java program. You will need to include the com.jcraft.jsch jar file. /* * SSHManager * * @author cabbott * @version 1.0 */ package cabbott.net; import com.jcraft.jsch.*; import java.io.IOException; import java.io.InputStream; import java.util.logging.Level; … Read more

“Invalid privatekey” when using JSch

Recent versions of OpenSSH (7.8 and newer) generate keys in new OpenSSH format by default, which starts with: —–BEGIN OPENSSH PRIVATE KEY—– JSch does not support this key format. You can use ssh-keygen to convert the key to the classic OpenSSH format: ssh-keygen -p -f <privateKeyFile> -m pem -P passphrase -N passphrase This “abuses” -p … Read more

com.jcraft.jsch.JSchException: UnknownHostKey

I would either: Try to ssh from the command line and accept the public key (the host will be added to ~/.ssh/known_hosts and everything should then work fine from Jsch) -OR- Configure JSch to not use “StrictHostKeyChecking” (this introduces insecurities and should only be used for testing purposes), using the following code: java.util.Properties config = … Read more