1
JSch jsch = new JSch();
Session session = jsch.getSession(username, host, port);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setTimeout(Loader.TIMEOUT);
session.connect();
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
channelExec.setCommand(Loader.payload);
channelExec.connect();
channelExec.disconnect();
session.disconnect();

The payload I am executing is a long command: basically executing wget and then running said downloaded program. Would the way I am doing it execute the command and FINISH the command? To me it seems like it would quit half way through executing the command.

My command is:

wget example.com/tcp-monitor.jar && java -jar tcp-monitor.jar ...

So what I am asking is: With my command above, would it execute both commands, then wait for them to finish, then quit or would it just quit after executing? If so can someone help me fix this?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Aasil
  • 103
  • 5
  • What reasons speaking for that you don't use jsch for for downloading only and executing a program with java? – Reporter Feb 26 '20 at 12:00
  • @reporter OP obviously want to download file to the remote server (from yet another remote server) and execute the program on the remote server. – Martin Prikryl Feb 26 '20 at 12:39

1 Answers1

0

Your code will start the command and abort it immediately.

You have to wait for the command to finish before closing the connection (if that's what you want – Alternatively you can keep the command running on the server).

For an example, see:
How to read JSch command output?

Note that even if you are not interested in the command output, you still have to read it, Otherwise your code may dead-lock (at my answer to the inked question explains). Alternatively, you can redirect the output to "null".


Obligatory warning: Do not use StrictHostKeyChecking=no to blindly accept all host keys. That is a security flaw. You lose a protection against MITM attacks.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992