Using JSch \"exec\" channel, I connect to a remote server and execute a command. What I can\'t do is to execute a list of commands stored in ArrayList. I use fo
You cannot call the setCommand multiple times. The "exec" channel can run a single "command" only.
But on most systems/shells, the "command" can actually include multiple commands.
A syntax will depend on your system/shell. Semicolons work usually:
((ChannelExec)channel).setCommand("hostname ; df -l");
Some servers/shells also allow newline.
((ChannelExec)channel).setCommand("hostname\ndf -l");
On *nix servers, you can also use && to make the following commands be executed only when the previous commands succeeded:
((ChannelExec)channel).setCommand("hostname && df -l");
See also Multiple commands through JSch shell.
Another option is to open a new channel for each command. Note that you can have multiple channels over a single SSH session. You do not have to reconnect for each channel/command.