“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

JSch Logs in files

Use Logger.log in the MyLogger.log: public void log(int level, String message){ LOGGER.log(loggerlevel, message); } A full code can be like: static private class MyJSchLogger implements com.jcraft.jsch.Logger { private java.util.logging.Logger logger; public MyJSchLogger(java.util.logging.Logger logger) { this.logger = logger; } public boolean isEnabled(int level){ return true; } public void log(int level, String message){ java.util.logging.Level l; switch (level) … Read more

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

Providing input/subcommands to command executed over SSH with JSch

Calling ChannelExec.setCommand multiple times has no effect. And even if it had, I’d guess that the 192.168.50.1 : and Config.txt are not commands, but inputs to the copy run tftp : command, aren’t they? If that’s the case, you need to write them to the command input. Something like this: ChannelExec channel = (ChannelExec) session.openChannel(“exec”); … 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

How to read JSch command output?

One has to read the output continuously, while waiting for the command to finish. Otherwise, if the command produces enough output to fill in an output buffer, the command will hang, waiting for the buffer to be consumed, what never happens. So you get a deadlock. The following example reads both stdout and stderr continuously, … Read more

Certain Unix commands fail with “… not found”, when executed through Java using JSch

The “exec” channel in the JSch (rightfully) does not allocate a pseudo terminal (PTY) for the session. As a consequence a different set of startup scripts is (might be) sourced (particularly for non-interactive sessions, .bash_profile is not sourced). And/or different branches in the scripts are taken, based on absence/presence of the TERM environment variable. So … Read more