Simple SSH connect with JSch

You have to execute that code in another thread so you don’t hang the UI thread is what that exception means. If the UI thread is executing a network call it can’t repaint the UI so your users sees a frozen UI that doesn’t respond to them while the app is waiting on the network … Read more

Sending commands to server via JSch shell channel

Try this: JSch jsch = new JSch(); try { Session session = jsch.getSession(“root”, “192.168.0.1”, 22); java.util.Properties config = new java.util.Properties(); config.put(“StrictHostKeyChecking”, “no”); session.setConfig(config); session.connect(); String command = “lsof -i :80”; Channel channel = session.openChannel(“exec”); ((ChannelExec) channel).setCommand(command); channel.setInputStream(null); ((ChannelExec) channel).setErrStream(System.err); InputStream in = channel.getInputStream(); channel.connect(); byte[] tmp = new byte[1024]; while (true) { while (in.available() > … Read more

How do I run SSH commands on remote system using Java? [closed]

Have a look at Runtime.exec() Javadoc Process p = Runtime.getRuntime().exec(“ssh myhost”); PrintStream out = new PrintStream(p.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); out.println(“ls -l /home/me”); while (in.ready()) { String s = in.readLine(); System.out.println(s); } out.println(“exit”); p.waitFor();

What is the difference between the ‘shell’ channel and the ‘exec’ channel in JSch

An overview about the differences and similarities between these streams you can find at »Shell, Exec or Subsystem Channel« in the JSch wiki. Here some details for your use case. In the exec channel, the commands come from the command string you did give with setCommand(). The SSH server will pass them at once to … 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

JSch Algorithm negotiation fail

As you can see, the server offers these ciphers: INFO: kex: server: aes256-cbc,aes192-cbc But JSch accepts only these: INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc There’s no common cipher to choose from. Note that JSch does support both aes256-cbc and aes192-cbc, but requires JCE (Java Cryptography Extension) to allow them. You probably do not have JCE, so these … Read more