Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the 'shell' channel and the 'exec' channel in JSch

Tags:

java

ssh

jsch

I want to be able to send many consecutive command represented as strings within a Java application to a SSH server for execution. Should I use:

Channel channel = session.openChannel("shell"); 

-or-

Channel channel = session.openChannel("exec"); 
like image 420
Martin Klosi Avatar asked Jul 21 '11 00:07

Martin Klosi


People also ask

What is a shell channel?

The shell channel is a black box with an input and output.


2 Answers

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 the shell (using something like bash -c '<command>').

They will all be executed, if the shell does not somehow exit before for some reason. (You could send a whole shell script here which implements some logic using if and similar, if this is wanted.)

So, to execute multiple commands, you could pass them to the exec channel by separating them with ; or newline (\n). As you can't wait for the results before giving all the commands, here you can only use multiple exec channels (but as each channel spawns a new shell, they don't conserve state between them, like working directory or shell variables).

In the shell channel, the shell will read input from the stream, and interpret the first line as a command (or several ones).

Then it will execute this command. The command itself might read more input from the stream, if it wants.

Then the shell will read the next line, interpret it as a command, and execute.

(In some cases the shell has to read more than one line, for example for long strings or composed commands like if or loops.)

This will go on until either the end of the stream (e.g. stream.close() at your side) or executing an explicit exit command.

If you don't send any input to the shell via the channels input/output stream, the shell will simply wait until you either send more or close the stream. Thus you can quietly read the output of one command, do some calculations on the client side and then decide which command to send next.

Just make sure you don't mix input to one command with the text of the next command - preferably don't use any commands which will read from standard input.

like image 122
Paŭlo Ebermann Avatar answered Oct 16 '22 17:10

Paŭlo Ebermann


With shell channel the shell (on unix it's sh or bash or something like that, on Windows it's usually cmd.exe) is started and console is created (the same you see on the screen if you run them locally). You have a prompt which you can parse or use for detection of completion of command.

With command channel a shell instance is started for each command (actually the channel is opened for each command) and a command is passed as a parameter for the shell (on Windows it would look like "cmd.exe /c ".

It's easier to use command channel cause you don't need to deal with command prompt.

like image 21
Eugene Mayevski 'Callback Avatar answered Oct 16 '22 16:10

Eugene Mayevski 'Callback