sshj icon indicating copy to clipboard operation
sshj copied to clipboard

Provision in session maintenance

Open saravanakumarvijayakumar opened this issue 4 years ago • 10 comments

Is there any method available for the following scenario?

  1. Connect to SFTP Server through sshj implementation and upload some files.
  2. Next time we need to upload one file. It should use the existing session and upload the file. The session created in first step should maintain for the particular time. The next upload files should just re-use the session. If there is no upload in the particular period, the session will be closed. The next upload should connect, authenticate and reuse the session.

You will need to program this yourself. SSHJ does not by itself handle the reuse for you.

Op vr 22 mei 2020 om 12:42 schreef saravanakumarvijayakumar < [email protected]>:

Is there any method available for the following scenario?

  1. Connect to SFTP Server through sshj implementation and upload some files.
  2. Next time we need to upload one file. It should use the existing session and upload the file. The session created in first step should maintain for the particular time. The next upload files should just re-use the session. If there is no upload in the particular period, the session will be closed. The next upload should connect, authenticate and reuse the session.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/hierynomus/sshj/issues/584, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA4XI3I244J6U7TQUGZ7WDRSZJITANCNFSM4NHVWCWA .

hierynomus avatar May 22 '20 11:05 hierynomus

Is there any provision for setting the idle timeout for session closing automatically?

This is the gentle reminder for the above question.

@saravanakumarvijayakumar No there isn't. There is a keep alive mechanism available to keep connections (and thus sessions) alive.

hierynomus avatar May 27 '20 20:05 hierynomus

I'm trying to do something similar as I would like to use exec to execute multiple commands sequentially over a single SSH session. Is this something that is supported or would this require changes to SSHJ code?

fmeum avatar Jun 10 '20 08:06 fmeum

@FabianHenneke You can open a shell for that :) Unless you don't know the commands beforehand, in which case I would suggest to cache the connected client by hostname.

hierynomus avatar Jun 10 '20 10:06 hierynomus

@FabianHenneke You can open a shell for that :) Unless you don't know the commands beforehand, in which case I would suggest to cache the connected client by hostname.

I guess opening a shell would be a decent workaround, but I would try to avoid that if possible. I may be doing something wrong, but the following attempt at reusing a connected client leads to a "multiple sessions" error:

SSHClient client = new SSHClient();
client.connect(...);
client.auth(...);
Session session = client.startSession();
session.exec(...);
session.close(...);
client.startSession(); // throws an exception

Is there a way to start a second session after closing the first one?

fmeum avatar Jun 10 '20 10:06 fmeum

No, at this moment the SSHClient is not reusable, which is also documented in the JavaDoc.

hierynomus avatar Jun 10 '20 10:06 hierynomus

I took the JavaDoc to mean that a Session is not reusable, but if it is indeed the SSHClient that is not reusable, this fully explains why I am seeing the exception. I will try to use the shell then.

fmeum avatar Jun 10 '20 10:06 fmeum

I took the JavaDoc to mean that a Session is not reusable, but if it is indeed the SSHClient that is not reusable, this fully explains why I am seeing the exception. I will try to use the shell then.

As it turns out, SSHClient (but not Session) seems to support reuse: https://github.com/android-password-store/Android-Password-Store/pull/1012

fmeum avatar Aug 12 '20 10:08 fmeum