1

I have series of commands that needs to be executed one after another on a remote vm. I also want to get the output of each command executed. This will be done with single JSch session. How can I achieve this?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Prasanta Biswas
  • 761
  • 2
  • 14
  • 26
  • Possible duplicate of [Multiple commands using Jsch](http://stackoverflow.com/questions/17352764/multiple-commands-using-jsch) – gonephishing Mar 16 '17 at 13:07

1 Answers1

0

To execute multiple commands, just use an appropriate syntax of your server. Most *nix server use semicolon or double-ampersand (with different semantics).

See Multiple commands using JSch.


Though to if your want to read commands output, you will have problem distinguishing, where output of one commands ends and output of the following commands starts.

Then it's better to execute each commands in its own "exec" channel. Single SSH session can have multiple channels opened (in sequence or even in parallel).

Channel channel1 = session.openChannel("exec");
((ChannelExec)channel1).setCommand(command1);
// ...

Channel channel2 = session.openChannel("exec");
((ChannelExec)channel2).setCommand(command2);
// ...

See also How to perform multiple operations with JSch.

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