5

I am new to SSH and JSch. When I connect from my client to the server I want to do two tasks:

  1. Upload a file (using ChannelSFTP)
  2. Perform commands, like creating a directory, and searching through a MySQL database

At the moment I am using two separate shell logins to perform each task (actually I haven't started programming the MySQL queries yet).

For the upload the relevant code is

session.connect();

Channel channel=session.openChannel("sftp");
channel.connect();
ChannelSftp c=(ChannelSftp)channel;
c.put(source, destination);

And for the command I have

String command = "ls -l";//just an example 
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);

Should I disconnect the session after the first channel and then open the second channel? Or close the session entirely and open a new session? As I said, I'm new to this.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • 1
    Just a note (as you said you are new to JSch): I created a [JavaDoc documentation](http://epaul.github.com/jsch-documentation/simple.javadoc/) of the classes, which might help. – Paŭlo Ebermann Sep 14 '11 at 16:29
  • Thank you. I'll have a look at it... –  Sep 14 '11 at 16:35

2 Answers2

8

One SSH session can support any number of channels - both in parallel and sequentially. (There is some theoretical limit in the channel identifier size, but you won't hit it in practice.) This is also valid for JSch. This saves redoing the costly key exchange operations.

So, there is normally no need to close the session and reconnect before opening a new channel. The only reason I can think about would be when you need to login with different credentials for both actions.

To safe some memory, you might want to close the SFTP channel before opening the exec channel, though.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • Are you talking about saving memory on the client(JSch) or the server(SSH server) ? – Muhammad Gelbana Sep 28 '12 at 08:05
  • I meant mainly the client (as this is what you control with JSch), but I suppose the server will also have some overhead for every open channel, including the SFTP server process in this case. – Paŭlo Ebermann Sep 28 '12 at 14:48
  • Do think the overhead of multiple channels on the server, specifically `exec` channels, would be considerable ? – Muhammad Gelbana Sep 30 '12 at 09:31
  • I have no idea (feel free to measure it yourself), but there generally is no reason to let a channel open after it is not used anymore. – Paŭlo Ebermann Sep 30 '12 at 09:33
3

To give multiple commands through Jsch use shell instead of exec. Shell only support native commands of the connecting system. For example, when you are connecting windows system you can't give commands like dir using the exec channel. So it is better to use shell.

The following code can be used to send multiple commands through Jsch

Channel channel = session.openChannel("shell");
OutputStream ops = channel.getOutputStream();
PrintStream ps = new PrintStream(ops, true);

channel.connect();
ps.println("mkdir folder");
ps.println("dir");
//give commands to be executed inside println.and can have any no of commands sent.
ps.close();

InputStream in = channel.getInputStream();
byte[] bt = new byte[1024];

while (true) {
    while (in.available() > 0) {
        int i = in.read(bt, 0, 1024);
        if (i < 0) {
            break;
        }
        String str = new String(bt, 0, i);
        //displays the output of the command executed.
        System.out.print(str);

    }
    if (channel.isClosed()) {
            break;
    }
    Thread.sleep(1000);
    channel.disconnect();
    session.disconnect();
}
svarog
  • 9,477
  • 4
  • 61
  • 77
krishna
  • 4,069
  • 2
  • 29
  • 56
  • 5
    No it's not better to use a shell channel. Using multiple exec channels is preferred approach. The shell channel is for implementing interactive sessions, not for automation. – Martin Prikryl Apr 12 '16 at 05:56