Multiple Commands Through Jsch Shell

SSH with JSch for multiple commands with intermediate Output

Do not use "shell" channel. The "shell" channel is intended to implement an interactive session (hence the welcome message), not to automate command execution.

To automate command execution, use "exec" channel. See Multiple commands through JSch shell.

Though you actually do not need multiple commands. There's no need for cd. Just use a full path in the cat command

ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand("cat " + path + "/some.properties");
channel.connect();

Though actually, if you want to read contents of files, use SFTP, instead of running console commands like cat. SFTP is a standardized API to access files over SFTP.

See SFTP file transfer using Java JSch.

Efficiently execute multiple commands that depend on another long running command on Unix host with JSch

What you have now is the correct way.

If you really really need to optimize it, you have to feed the commands to a remote shell session. Either start a shell explicitly using the "exec" channel. Or use "shell" channel directly. And then write the individual commands to the shell input.

See also:

  • Providing input/subcommands to command executed over SSH with JSch
  • What is the difference between the 'shell' channel and the 'exec' channel in JSch
  • Multiple commands through JSch shell
  • JSch Shell channel execute commands one by one testing result before proceeding

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. For the correct (and secure) approach, see: How to resolve Java UnknownHostKey, while using JSch SFTP library?

execute multiple command with jsch session and channelexec

Thank you to Nicholas and Martin for their responses. I figured out what was wrong and wanted to post an answer as I do realize that little things like this do crop up for us 'dumb' programmers out there who ask ridiculous questions that incur negative votes. The output for the second command was returning a warning / error response in the first line and by not including the following I was not seeing that and reading the next line was empty. I know it's stupid and should have figured this out before posting because that is the point of this site: post questions that are beyond the knowledge of others. But since I should have innately known this:

Anyway ensure the following line is included:

  ((ChannelExec)channelII).setErrStream(System.err);

and also read the stream with a loop and not just test with reading the first line.

     while ((scriptOutput = scriptReader.readLine())!= null) {
sb.append(scriptOutput + "\n");
}

I hope this can at least be a lesson to some if not a solution.

Performance takes a hit while using Java JSch exec vs shell to run multiple commands?

Yes, that's quite likely. The "exec" channel runs the command in an isolated environment (typically a new shell instance).

Setting up the environment on the server will likely take some time.

So it most likely has nothing to do with your Java code.



Related Topics



Leave a reply



Submit