2

I'm having a requirement where I need to open one SftpChannel for file transfer and one exec channel to execute the commands on remote system.So,Can I open these channels using a single jsch object and single session object.If I can please give me a small code snippet or please do suggest me is there any other way to do this?

Thushi
  • 188
  • 1
  • 13
  • Include an executable version of your code and you are more likely to receive a completed working snippet. See http://stackoverflow.com/help/mcve – Damienknight Jun 11 '14 at 15:02

1 Answers1

1

You can have multiple open channels on a single session.

  Channel chExec = session.openChannel("exec");
  Channel chSFTP = session.openChannel("sftp");

  chExec.setInputStream(System.in);
  chExec.setOutputStream(System.out);
  chSFTP.setInputStream(System.in);
  chSFTP.setOutputStream(System.out);

  chExec.connect();
  chSFTP.connect();

In the above snippet, you would be sending all System input into both channels, which you probably dont want to do, so you will have to create a unique stream for one or both of the Channels.

Also, you could open a channel, use it, close it, then open a new channel, all inside the same session.

Jsch does not automatically close your sessions. You must explicitly close them. See this answer for an explanation on closing your channels and sessions.

Community
  • 1
  • 1
Damienknight
  • 1,876
  • 2
  • 18
  • 34
  • Can you have multiple channels of the same type("sftp")? – Roland May 10 '17 at 13:35
  • @Roland I've tried this, I think it's not allowed to do so. The underlying session received an message(message type = SSH_MSG_CHANNEL_OPEN_FAILURE) from the Ubuntu server while I was trying to open the second SFTP channel from the same session. – Robb Tsang Feb 22 '18 at 08:42
  • 1
    @Roland Sorry for the previous incorrect comment. I've took a look at the server side settings and find out why I can't open the second SFTP channel. In fact we can have multiple sftp channels, but it's limited by the server side `MaxSessions` option in `/etc/ssh/sshd_config`. If the `MaxSessions` is set to 1, then only one channel can be opened. – Robb Tsang Feb 22 '18 at 09:26