File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Linux / UNIX and the fly likes JSCH session handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "JSCH session handling" Watch "JSCH session handling" New topic
Author

JSCH session handling

Dorothy Taylor
Ranch Hand

Joined: Nov 26, 2007
Posts: 104
Hi

I am using JSCH library for executing commands in Linux. Please advise if we can use this to execute multiple commands in a single session. Further, what if these commands have to be executed in parallel using JSCH? Is it possible and how?

Thanks
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

To execute multiple commands just create a "shell" channel from a session and write the commands to the shell stdin. The Shell.java example is a starting point.

To execute in parallel you have two options. First, assuming a *Nix server then you can execute each command using Shell channel sent to the shell stdin terminated by a & . or second you can create a number of sessions each in it's own Java thread and then create an ChannelExec from the session for a command .
Dorothy Taylor
Ranch Hand

Joined: Nov 26, 2007
Posts: 104
So this means that both shell and exec channels can be used for multiple commands and also for executing in parallel, the only difference being that one can store state while the other cannot. If this is the only difference then why do we need two options?
Dorothy Taylor
Ranch Hand

Joined: Nov 26, 2007
Posts: 104
If I use 'exec' channel for multiple commands, how can I know the output of a particular command from the group?
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

Dorothy Taylor wrote:If I use 'exec' channel for multiple commands, how can I know the output of a particular command from the group?


You can't so you would want to use multiple exec(0 commands! This of course answers part of your question in your previous response.

To my mind the major reason for having both the shell() and the exec() operations is that the shell() output is designed to interact with humans through a terminal and can be decorated with terminal control characters but exec() just returns the raw output. Even this is not the whole story since implementations such as OpenSSH allow one to specify no terminal (the -T command line option) .

Though there is a set of RFC that specify SSH these seem to have been written to try to formalize an existing SSH implementation written before the specification was considered. The RFC leave much unsaid and in places are ambiguous; this is especially true when one looks at key re-negotiation where at times the role of client and server seem to be mixed up. Don't get me started on the specifications for SFTP and SCP .

Dorothy Taylor
Ranch Hand

Joined: Nov 26, 2007
Posts: 104
Also please validate my understanding that even with 'shell' channel, to know the output of any one command from a sequence of multiple commands, we will need to parse the output stream. Even in this case, how will we know which output corresponds to which command, since all are clubbed together only
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

Dorothy Taylor wrote:Also please validate my understanding that even with 'shell' channel, to know the output of any one command from a sequence of multiple commands, we will need to parse the output stream. Even in this case, how will we know which output corresponds to which command, since all are clubbed together only


Of course the outputs of the parallel running tasks will be mangled together if you use just one shell command ! Even if they are not running in parallel but one after the other in the same exec() one would have to separate the outputs. So for each command run separate exec() though separate connections each using a separate client Java thread .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSCH session handling
 
Similar Threads
Moving files from server A to server B using JSch
JSP page to call unix scrpit
Jsch Passphrase Query
connecting to UNIX
Methods for threads?