8

I'm looking to move small files (at unknown times and in unknown quantities) from one server to another using JSch - unfortunately, I tried to open up a session for each transfer which quickly bombed out (as MaxSessions was I guess 10?).

Then I opened one session (per host) with JSch, and simply created a channel for each action (a single send would create a channel, put the file, closed the channel), but I encountered the same thing (when trying to do it 10 or more at a time, I would get connections refused as some channels were still closing when another request came through).

So I've looked at sshd_config and figured out that they have a MaxSessions property which would come in quite useful here - but it got me wondering, is there not a MaxChannelsPerSession property, or am I getting confused with regard to what JSch calls a "channel" and what is an actual SSH channel.

I am using the most recent version of JSch as of 20120316.

(And most importantly, should I post this in a different place on the site? It's kind of programming related, but I guess is part sysadmin as well...)

SubSevn
  • 1,008
  • 2
  • 10
  • 27

2 Answers2

9

I'm the author of JSch.

"MaxSessions" property on sshd_config will be recognized as "MaxChannelsPerSession" on OpenSSH's sshd.

ymnk
  • 1,145
  • 7
  • 7
  • 8
    A slight follow-up: is a session/channel thread safe? Could I create a session to be held globally (or a reference passed around) and then create channels with it in different threads without worrying, or should I be putting it all in a syncrhonized block? – SubSevn Mar 20 '12 at 12:32
  • @ymnk can you please check out this SO: http://stackoverflow.com/questions/23692643/jsch-sftp-channel-state-impact-by-previous-channel-state-of-the-same-session – Gelin Luo May 16 '14 at 05:11
1

Just for another alternative:

For transfering files, JSch offers the ChannelSftp class. With it you can transfer multiple files one after another (or even in parallel) using only one channel (for each server).

I don't know if this offers any performance benefits, though.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • 2
    Have you seen any examples of using a single ChannelSftp instance to transfer files in parallel ? I have been trying to dig one up. – Nicholas May 24 '13 at 13:44
  • No, my knowledge of ChannelSftp is mostly theoretic in nature, by reading its code and the SFTP pre-RFC. – Paŭlo Ebermann May 24 '13 at 19:37
  • Off topic, but is there an equivalent for `Apache Mina SSHD`'s client? I use this rather than `JSCH` and my app would benefit from a reusable channel. Currently I'm connecting and disconnecting for every single file which has a huge overhead. – Sridhar Sarnobat Aug 07 '15 at 20:19
  • 1
    @Sridhar-Sarnobat I did never use that, but from a look at the documentation, I would say [DefaultSftpClient](https://mina.apache.org/sshd-project/apidocs/index.html?org/apache/sshd/client/sftp/DefaultSftpClient.html) represents a SFTP channel from the client side, and its read/write methods for transferring can be used multiple times. – Paŭlo Ebermann Aug 09 '15 at 07:28
  • Ah I see. I guess my existing code is already optimized then (though maybe I need to avoid calling stop or close at all levels (session, ssh client, sftp client). Thanks. – Sridhar Sarnobat Aug 09 '15 at 15:59