Interface ClientSession

All Superinterfaces:
org.apache.sshd.common.AttributeRepository, org.apache.sshd.common.AttributeStore, AutoCloseable, Channel, ChannelListenerManager, ChannelStreamWriterResolver, ChannelStreamWriterResolverManager, ClientAuthenticationManager, Closeable, org.apache.sshd.common.Closeable, org.apache.sshd.common.util.net.ConnectionEndpointsIndicator, FactoryManagerHolder, KexExtensionHandlerManager, KexFactoryManager, org.apache.sshd.common.keyprovider.KeyIdentityProviderHolder, org.apache.sshd.common.auth.MutableUserHolder, PortForwardingEventListenerManager, PortForwardingInformationProvider, PortForwardingManager, org.apache.sshd.common.PropertyResolver, ReservedSessionMessagesManager, Session, org.apache.sshd.common.session.SessionContext, SessionDisconnectHandlerManager, org.apache.sshd.common.session.SessionHeartbeatController, SessionListenerManager, org.apache.sshd.common.signature.SignatureFactoriesHolder, org.apache.sshd.common.signature.SignatureFactoriesManager, UnknownChannelReferenceHandlerManager, org.apache.sshd.common.auth.UserAuthFactoriesManager<ClientSession, UserAuth, UserAuthFactory>, org.apache.sshd.common.auth.UsernameHolder
All Known Implementing Classes:
AbstractClientSession, ClientSessionImpl

public interface ClientSession extends Session, ClientAuthenticationManager, PortForwardingManager

An authenticated session to a given SSH server.

A client session is established using the SshClient. Once the session has been created, the user has to authenticate using either ClientAuthenticationManager.addPasswordIdentity(String) or ClientAuthenticationManager.addPublicKeyIdentity(java.security.KeyPair) followed by a call to auth().

From this session, channels can be created using the createChannel(String) method. Multiple channels can be created on a given session concurrently.

When using the client in an interactive mode, the waitFor(Collection, long) method can be used to listen to specific events such as the session being established, authenticated or closed.

When a given session is no longer used, it must be closed using the Closeable.close(boolean) method.
Author:
Apache MINA SSHD Project
  • Field Details

  • Method Details

    • getConnectAddress

      SocketAddress getConnectAddress()
      Returns the original address (after having been translated through host configuration entries if any) that was request to connect. It contains the original host or address string that was used. Note: this may be different than the result of the Session.getIoSession() report of the remote peer
      Returns:
      The original requested address
    • getHostConfigEntry

      org.apache.sshd.client.config.hosts.HostConfigEntry getHostConfigEntry()
      Retrieves the HostConfigEntry that was used to create this session.
      Returns:
      the HostConfigEntry; or null if none
    • getConnectionContext

      org.apache.sshd.common.AttributeRepository getConnectionContext()
      Returns:
      The "context" data provided when session connection was established - null if none.
    • auth

      AuthFuture auth() throws IOException
      Starts the authentication process. User identities will be tried until the server successfully authenticate the user. User identities must be provided before calling this method using ClientAuthenticationManager.addPasswordIdentity(String) or ClientAuthenticationManager.addPublicKeyIdentity(java.security.KeyPair).
      Returns:
      the authentication future
      Throws:
      IOException - if failed to generate the future
      See Also:
    • getServerKey

      PublicKey getServerKey()
      Retrieves the server's host key: the one it presented in the last key exchange.
      Returns:
      the server's PublicKey; may be null if no key exchange has been completed yet
    • registerHostKey

      void registerHostKey(PublicKey hostKey)
      Explicitly registers a known host key for the session. Intended to be used if the server announces additional host keys as part of the OpenSSH "hostkeys-00@openssh.com" extension, but it can also be used to register any host key at any other time.

      If the server presents a host key that matches any of the ones registered in the session, the key will be accepted implicitly without consulting the ServerKeyVerifier.

      If the hostKey is a host certificate, the CA key will be registered. When the server presents a certificate, the certificate will be checked in any case and will be rejected if it is invalid or expired. If it is valid and not expired, it will be accepted implicitly if its CA key is already registered.

      Parameters:
      hostKey - the host key to register
      Throws:
      IllegalArgumentException - if hostKey is a user certificate
    • getRegisteredHostKeys

      Collection<String> getRegisteredHostKeys()
      Obtains a read-only collection of all currently registered server host keys is OpenSSH format, i.e. key type as a string, followed by the base64-encoded key, prefixed with "@cert-authority" if it's a CA key.

      For a normal key: "key-type AAAA..."; for a host certificate CA key "@cert-authority key-type AAAA...".

      Note that this is not quite the format used in a OpenSSH known_hosts file as there are no host patterns.

      Returns:
      all currently registered server host keys; never null but possibly empty, and never containing null
    • createChannel

      ClientChannel createChannel(String type) throws IOException
      Create a channel of the given type. Same as calling createChannel(type, null).
      Parameters:
      type - The channel type
      Returns:
      The created ClientChannel
      Throws:
      IOException - If failed to create the requested channel
    • createChannel

      ClientChannel createChannel(String type, String subType) throws IOException
      Create a channel of the given type and sub-type.
      Parameters:
      type - The channel type
      subType - The channel sub-type
      Returns:
      The created ClientChannel
      Throws:
      IOException - If failed to create the requested channel
    • createShellChannel

      default ChannelShell createShellChannel() throws IOException
      Create a channel to start a shell using default PTY settings and environment.
      Returns:
      The created ChannelShell
      Throws:
      IOException - If failed to create the requested channel
    • createShellChannel

      ChannelShell createShellChannel(org.apache.sshd.common.channel.PtyChannelConfigurationHolder ptyConfig, Map<String,?> env) throws IOException
      Create a channel to start a shell using specific PTY settings and/or environment.
      Parameters:
      ptyConfig - The PTY configuration to use - if null then internal defaults are used
      env - Extra environment configuration to be transmitted to the server - ignored if null/empty.
      Returns:
      The created ChannelShell
      Throws:
      IOException - If failed to create the requested channel
    • createExecChannel

      default ChannelExec createExecChannel(String command) throws IOException
      Create a channel to execute a command using default PTY settings and environment.
      Parameters:
      command - The command to execute
      Returns:
      The created ChannelExec
      Throws:
      IOException - If failed to create the requested channel
    • createExecChannel

      default ChannelExec createExecChannel(String command, org.apache.sshd.common.channel.PtyChannelConfigurationHolder ptyConfig, Map<String,?> env) throws IOException
      Create a channel to execute a command using specific PTY settings and/or environment.
      Parameters:
      command - The command to execute
      ptyConfig - The PTY configuration to use - if null then internal defaults are used
      env - Extra environment configuration to be transmitted to the server - ignored if null/empty.
      Returns:
      The created ChannelExec
      Throws:
      IOException - If failed to create the requested channel
    • createExecChannel

      ChannelExec createExecChannel(String command, Charset charset, org.apache.sshd.common.channel.PtyChannelConfigurationHolder ptyConfig, Map<String,?> env) throws IOException
      Create a channel to execute a command using specific PTY settings and/or environment.
      Parameters:
      command - The command to execute
      charset - The Charset to use for sending the command string
      ptyConfig - The PTY configuration to use - if null then internal defaults are used
      env - Extra environment configuration to be transmitted to the server - ignored if null/empty.
      Returns:
      The created ChannelExec
      Throws:
      IOException - If failed to create the requested channel
    • createExecChannel

      ChannelExec createExecChannel(byte[] command, org.apache.sshd.common.channel.PtyChannelConfigurationHolder ptyConfig, Map<String,?> env) throws IOException
      Create a channel to execute a command using specific PTY settings and/or environment.
      Parameters:
      command - The command to execute
      ptyConfig - The PTY configuration to use - if null then internal defaults are used
      env - Extra environment configuration to be transmitted to the server - ignored if null/empty.
      Returns:
      The created ChannelExec
      Throws:
      IOException - If failed to create the requested channel
    • executeRemoteCommand

      default String executeRemoteCommand(String command) throws IOException
      Execute a command that requires no input and returns its output
      Parameters:
      command - The command to execute
      Returns:
      The command's standard output result (assumed to be in US-ASCII)
      Throws:
      IOException - If failed to execute the command - including if anything was written to the standard error or a non-zero exit status was received. If this happens, then a RemoteException is thrown with a cause of ServerException containing the remote captured standard error - including CR/LF(s)
      See Also:
    • executeRemoteCommand

      default String executeRemoteCommand(String command, Duration timeout) throws IOException
      Execute a command that requires no input and returns its output
      Parameters:
      command - The command to execute
      timeout - Timeout for the remote command execution. Applies to both channel opening and result waiting. A zero or negative value means no timeout.
      Returns:
      The command's standard output result
      Throws:
      IOException - If failed to execute the command - including if anything was written to the standard error or a non-zero exit status was received. If this happens, then a RemoteException is thrown with a cause of ServerException containing the remote captured standard error - including CR/LF(s)
      See Also:
    • executeRemoteCommand

      default String executeRemoteCommand(String command, OutputStream stderr, Charset charset) throws IOException
      Execute a command that requires no input and returns its output
      Parameters:
      command - The command to execute - without a terminating LF
      stderr - Standard error output stream - if null then error stream data is ignored. Note: if the stream is not null then it will be left open when this method returns or exception is thrown
      charset - The command Charset for input/output/error - if null then US_ASCII is assumed
      Returns:
      The command's standard output result
      Throws:
      IOException - If failed to manage the command channel - Note: the code does not check if anything was output to the standard error stream, but does check the reported exit status (if any) for non-zero value. If non-zero exit status received then a RemoteException is thrown with' a ServerException cause containing the exits value
      See Also:
    • executeRemoteCommand

      default String executeRemoteCommand(String command, OutputStream stderr, Charset charset, Duration timeout) throws IOException
      Execute a command that requires no input and returns its output
      Parameters:
      command - The command to execute - without a terminating LF
      stderr - Standard error output stream - if null then error stream data is ignored. Note: if the stream is not null then it will be left open when this method returns or exception is thrown
      charset - The command Charset for input/output/error - if null then US_ASCII is assumed
      timeout - Timeout for the remote command execution. Applies to both channel opening and result waiting. A zero or negative value means no timeout.
      Returns:
      The command's standard output result
      Throws:
      IOException - If failed to manage the command channel - Note: the code does not check if anything was output to the standard error stream, but does check the reported exit status (if any) for non-zero value. If non-zero exit status received then a RemoteException is thrown with' a ServerException cause containing the exits value
      See Also:
    • executeRemoteCommand

      default void executeRemoteCommand(String command, OutputStream stdout, OutputStream stderr, Charset charset) throws IOException
      Execute a command that requires no input and redirects its STDOUT/STDERR streams to the user-provided ones
      Parameters:
      command - The command to execute - without a terminating LF
      stdout - Standard output stream - if null then stream data is ignored. Note: if the stream is not null then it will be left open when this method returns or exception is thrown
      stderr - Error output stream - if null then stream data is ignored. Note: if the stream is not null then it will be left open when this method returns or exception is thrown
      charset - The command Charset for output/error - if null then US_ASCII is assumed
      Throws:
      IOException - If failed to execute the command or got a non-zero exit status
      See Also:
    • executeRemoteCommand

      default void executeRemoteCommand(String command, OutputStream stdout, OutputStream stderr, Charset charset, Duration timeout) throws IOException
      Execute a command that requires no input and redirects its STDOUT/STDERR streams to the user-provided ones
      Parameters:
      command - The command to execute - without a terminating LF.
      stdout - Standard output stream - if null then stream data is ignored. Note: if the stream is not null, it will be left open when this method returns or an exception is thrown.
      stderr - Error output stream - if null then error stream data is ignored. Note: if the stream is not null, it will be left open when this method returns or an exception is thrown.
      charset - The charset to use for encoding the command and decoding the output/error streams. If null, US-ASCII is assumed.
      timeout - Timeout for the remote command execution. Applies to both channel opening and result waiting. A zero or negative value means no timeout.
      Throws:
      IOException - If the command execution fails, times out, or returns a non-zero exit code. A RemoteException may be thrown if the remote side reports an error.
      See Also:
    • createSubsystemChannel

      ChannelSubsystem createSubsystemChannel(String subsystem) throws IOException
      Create a subsystem channel.
      Parameters:
      subsystem - The subsystem name
      Returns:
      The created ChannelSubsystem
      Throws:
      IOException - If failed to create the requested channel
    • createDirectTcpipChannel

      ChannelDirectTcpip createDirectTcpipChannel(org.apache.sshd.common.util.net.SshdSocketAddress local, org.apache.sshd.common.util.net.SshdSocketAddress remote) throws IOException
      Create a direct tcp-ip channel which can be used to stream data to a remote port from the server.
      Parameters:
      local - The local address
      remote - The remote address
      Returns:
      The created ChannelDirectTcpip
      Throws:
      IOException - If failed to create the requested channel
    • createLocalPortForwardingTracker

      default ExplicitPortForwardingTracker createLocalPortForwardingTracker(int localPort, org.apache.sshd.common.util.net.SshdSocketAddress remote) throws IOException
      Starts a local port forwarding and returns a tracker that stops the forwarding when the close() method is called. This tracker can be used in a try-with-resource block to ensure cleanup of the set up forwarding.
      Parameters:
      localPort - The local port - if zero one is allocated
      remote - The remote address
      Returns:
      The tracker instance
      Throws:
      IOException - If failed to set up the requested forwarding
      See Also:
    • createLocalPortForwardingTracker

      default ExplicitPortForwardingTracker createLocalPortForwardingTracker(org.apache.sshd.common.util.net.SshdSocketAddress local, org.apache.sshd.common.util.net.SshdSocketAddress remote) throws IOException
      Starts a local port forwarding and returns a tracker that stops the forwarding when the close() method is called. This tracker can be used in a try-with-resource block to ensure cleanup of the set up forwarding.
      Parameters:
      local - The local address
      remote - The remote address
      Returns:
      The tracker instance
      Throws:
      IOException - If failed to set up the requested forwarding
      See Also:
    • createRemotePortForwardingTracker

      default ExplicitPortForwardingTracker createRemotePortForwardingTracker(org.apache.sshd.common.util.net.SshdSocketAddress remote, org.apache.sshd.common.util.net.SshdSocketAddress local) throws IOException
      Starts a remote port forwarding and returns a tracker that stops the forwarding when the close() method is called. This tracker can be used in a try-with-resource block to ensure cleanup of the set up forwarding.
      Parameters:
      remote - The remote address
      local - The local address
      Returns:
      The tracker instance
      Throws:
      IOException - If failed to set up the requested forwarding
      See Also:
    • createDynamicPortForwardingTracker

      default DynamicPortForwardingTracker createDynamicPortForwardingTracker(org.apache.sshd.common.util.net.SshdSocketAddress local) throws IOException
      Starts a dynamic port forwarding and returns a tracker that stops the forwarding when the close() method is called. This tracker can be used in a try-with-resource block to ensure cleanup of the set up forwarding.
      Parameters:
      local - The local address
      Returns:
      The tracker instance
      Throws:
      IOException - If failed to set up the requested forwarding
      See Also:
    • getSessionState

      Returns:
      A snapshot of the current session state
      See Also:
    • waitFor

      Wait for any one of a specific state to be signaled.
      Parameters:
      mask - The request ClientSession.ClientSessionEvents mask
      timeout - Wait time in milliseconds - non-positive means forever
      Returns:
      The actual state that was detected either due to the mask yielding one of the states or due to timeout (in which case the ClientSession.ClientSessionEvent.TIMEOUT value is set)
    • waitFor

      Wait for any one of a specific state to be signaled.
      Parameters:
      mask - The request ClientSession.ClientSessionEvents mask
      timeout - Wait time - null means forever
      Returns:
      The actual state that was detected either due to the mask yielding one of the states or due to timeout (in which case the ClientSession.ClientSessionEvent.TIMEOUT value is set)
    • getMetadataMap

      Map<Object,Object> getMetadataMap()
      Access to the metadata.
      Returns:
      The metadata Map - Note: access to the map is not synchronized in any way - up to the user to take care of mutual exclusion if necessary
    • getFactoryManager

      ClientFactoryManager getFactoryManager()
      Specified by:
      getFactoryManager in interface FactoryManagerHolder
      Returns:
      The ClientFactoryManager for this session.
    • switchToNoneCipher

      KeyExchangeFuture switchToNoneCipher() throws IOException

      Switch to a none cipher for performance.

      This should be done after the authentication phase has been performed. After such a switch, interactive channels are not allowed anymore. Both client and server must have been configured to support the none cipher. If that's not the case, the returned future will be set with an exception.

      Returns:
      an KeyExchangeFuture that can be used to wait for the exchange to be finished
      Throws:
      IOException - if a key exchange is already running
    • providerOf

      static org.apache.sshd.common.keyprovider.KeyIdentityProvider providerOf(ClientSession session)
      Creates a "unified" KeyIdentityProvider of key pairs out of the registered KeyPair identities and the extra available ones as a single iterator of key pairs
      Parameters:
      session - The ClientSession - ignored if null (i.e., empty iterator returned)
      Returns:
      The wrapping KeyIdentityProvider
      See Also:
    • passwordIteratorOf

      static Iterator<String> passwordIteratorOf(ClientSession session) throws IOException, GeneralSecurityException
      Creates a "unified" Iterator of passwords out of the registered passwords and the extra available ones as a single iterator of passwords
      Parameters:
      session - The ClientSession - ignored if null (i.e., empty iterator returned)
      Returns:
      The wrapping iterator
      Throws:
      IOException - If failed to load the passwords
      GeneralSecurityException - If some security issue with the passwords
      See Also: