You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by Claude Warren <cl...@xenei.com> on 2016/11/17 14:57:50 UTC

SSHD client - is it possible to reuse a session?

I am attempting to create a client session and then use it to send multiple
commands.  Is this possible?

{noformat}

           ConnectFuture connect = client.connect(cred.getUserName(),
                    socketAddress);
            if (connect.await(10000)) {
                session = connect.getSession();
                if (session != null) {
                    session.addPasswordIdentity(cred.getPassword());
                    AuthFuture future = session.auth();
                    future.await(config.getConnectionTimeout());
                    if (future.isFailure()) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug(String.format(
                                    "Login to %s with id '%s' failed",
                                    socketAddress, cred.getUserName()));
                            session.close(false);
                        }
                    } else {

                        return session;
                    }
                } else {
                    LOG.info(String.format("Unable to create session with
%s",
                            socketAddress));
                }

 === snip ===

              ClientChannel channel =
session.createChannel(Channel.CHANNEL_EXEC,
                        command.getSend());
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                ByteArrayOutputStream err = new ByteArrayOutputStream();
                channel.setOut(out);
                channel.setErr(err);

                channel.open().await(10000);
                channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), 0);

{noformat}

Basically I call the first part to create the session and then execute the
command in the second part.  I process the results and depending on output
I run other commands by calling the second part again. (same session, and I
have closed the previous channel).

The issue I see is that the session is closed when the channel is closed.
The implementation is using the NIO2 packages.

I am not certiain if this is a bug,
a misconfiguraiton on my part,
or patently not possible.

Any assistance would be appreciated.

Claude Warren
-- 
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren

Re: SSHD client - is it possible to reuse a session?

Posted by Claude Warren <cl...@xenei.com>.
It seems that the channel close causes the session to close as well.

I will see if I can work up a simple, short, contained example and open a
defect.

Claude

On Thu, Nov 17, 2016 at 9:23 PM, Guillaume Nodet <gn...@apache.org> wrote:

> It's a valid use case and it should work.
>
> However, if you stack your sessions for too long, the client or server may
> consider the session idle for too long and close it.
> Before reusing the session, check the session state with a call isOpen().
> You can change the timeout on the client or server (if using sshd) with the
> following code.  The default value is FactoryManager.DEFAULT_IDLE_TIMEOUT
> with
> is 10 minutes.
>
> final long idleTimeoutValue = TimeUnit.SECONDS.toMillis(5L);
> PropertyResolverUtils.updateProperty(sshd,
> FactoryManager.IDLE_TIMEOUT, idleTimeoutValue);
>
>
> 2016-11-17 15:57 GMT+01:00 Claude Warren <cl...@xenei.com>:
>
> > I am attempting to create a client session and then use it to send
> multiple
> > commands.  Is this possible?
> >
> > {noformat}
> >
> >            ConnectFuture connect = client.connect(cred.getUserName(),
> >                     socketAddress);
> >             if (connect.await(10000)) {
> >                 session = connect.getSession();
> >                 if (session != null) {
> >                     session.addPasswordIdentity(cred.getPassword());
> >                     AuthFuture future = session.auth();
> >                     future.await(config.getConnectionTimeout());
> >                     if (future.isFailure()) {
> >                         if (LOG.isDebugEnabled()) {
> >                             LOG.debug(String.format(
> >                                     "Login to %s with id '%s' failed",
> >                                     socketAddress, cred.getUserName()));
> >                             session.close(false);
> >                         }
> >                     } else {
> >
> >                         return session;
> >                     }
> >                 } else {
> >                     LOG.info(String.format("Unable to create session with
> > %s",
> >                             socketAddress));
> >                 }
> >
> >  === snip ===
> >
> >               ClientChannel channel =
> > session.createChannel(Channel.CHANNEL_EXEC,
> >                         command.getSend());
> >                 ByteArrayOutputStream out = new ByteArrayOutputStream();
> >                 ByteArrayOutputStream err = new ByteArrayOutputStream();
> >                 channel.setOut(out);
> >                 channel.setErr(err);
> >
> >                 channel.open().await(10000);
> >                 channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED),
> 0);
> >
> > {noformat}
> >
> > Basically I call the first part to create the session and then execute
> the
> > command in the second part.  I process the results and depending on
> output
> > I run other commands by calling the second part again. (same session,
> and I
> > have closed the previous channel).
> >
> > The issue I see is that the session is closed when the channel is closed.
> > The implementation is using the NIO2 packages.
> >
> > I am not certiain if this is a bug,
> > a misconfiguraiton on my part,
> > or patently not possible.
> >
> > Any assistance would be appreciated.
> >
> > Claude Warren
> > --
> > I like: Like Like - The likeliest place on the web
> > <http://like-like.xenei.com>
> > LinkedIn: http://www.linkedin.com/in/claudewarren
> >
>
>
>
> --
> ------------------------
> Guillaume Nodet
> ------------------------
> Red Hat, Open Source Integration
>
> Email: gnodet@redhat.com
> Web: http://fusesource.com
> Blog: http://gnodet.blogspot.com/
>



-- 
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren

Re: SSHD client - is it possible to reuse a session?

Posted by Guillaume Nodet <gn...@apache.org>.
It's a valid use case and it should work.

However, if you stack your sessions for too long, the client or server may
consider the session idle for too long and close it.
Before reusing the session, check the session state with a call isOpen().
You can change the timeout on the client or server (if using sshd) with the
following code.  The default value is FactoryManager.DEFAULT_IDLE_TIMEOUT with
is 10 minutes.

final long idleTimeoutValue = TimeUnit.SECONDS.toMillis(5L);
PropertyResolverUtils.updateProperty(sshd,
FactoryManager.IDLE_TIMEOUT, idleTimeoutValue);


2016-11-17 15:57 GMT+01:00 Claude Warren <cl...@xenei.com>:

> I am attempting to create a client session and then use it to send multiple
> commands.  Is this possible?
>
> {noformat}
>
>            ConnectFuture connect = client.connect(cred.getUserName(),
>                     socketAddress);
>             if (connect.await(10000)) {
>                 session = connect.getSession();
>                 if (session != null) {
>                     session.addPasswordIdentity(cred.getPassword());
>                     AuthFuture future = session.auth();
>                     future.await(config.getConnectionTimeout());
>                     if (future.isFailure()) {
>                         if (LOG.isDebugEnabled()) {
>                             LOG.debug(String.format(
>                                     "Login to %s with id '%s' failed",
>                                     socketAddress, cred.getUserName()));
>                             session.close(false);
>                         }
>                     } else {
>
>                         return session;
>                     }
>                 } else {
>                     LOG.info(String.format("Unable to create session with
> %s",
>                             socketAddress));
>                 }
>
>  === snip ===
>
>               ClientChannel channel =
> session.createChannel(Channel.CHANNEL_EXEC,
>                         command.getSend());
>                 ByteArrayOutputStream out = new ByteArrayOutputStream();
>                 ByteArrayOutputStream err = new ByteArrayOutputStream();
>                 channel.setOut(out);
>                 channel.setErr(err);
>
>                 channel.open().await(10000);
>                 channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), 0);
>
> {noformat}
>
> Basically I call the first part to create the session and then execute the
> command in the second part.  I process the results and depending on output
> I run other commands by calling the second part again. (same session, and I
> have closed the previous channel).
>
> The issue I see is that the session is closed when the channel is closed.
> The implementation is using the NIO2 packages.
>
> I am not certiain if this is a bug,
> a misconfiguraiton on my part,
> or patently not possible.
>
> Any assistance would be appreciated.
>
> Claude Warren
> --
> I like: Like Like - The likeliest place on the web
> <http://like-like.xenei.com>
> LinkedIn: http://www.linkedin.com/in/claudewarren
>



-- 
------------------------
Guillaume Nodet
------------------------
Red Hat, Open Source Integration

Email: gnodet@redhat.com
Web: http://fusesource.com
Blog: http://gnodet.blogspot.com/