Class SshClient

java.lang.Object
org.apache.sshd.common.util.logging.AbstractLoggingBean
org.apache.sshd.common.util.closeable.IoBaseCloseable
org.apache.sshd.common.util.closeable.AbstractCloseable
org.apache.sshd.common.util.closeable.AbstractInnerCloseable
All Implemented Interfaces:
Closeable, AutoCloseable, Channel, ClientAuthenticationManager, ClientFactoryManager, org.apache.sshd.client.config.keys.ClientIdentityLoaderHolder, org.apache.sshd.client.config.keys.ClientIdentityLoaderManager, ClientSessionCreator, org.apache.sshd.common.AttributeRepository, org.apache.sshd.common.AttributeStore, org.apache.sshd.common.auth.UserAuthFactoriesManager<ClientSession, UserAuth, UserAuthFactory>, ChannelListenerManager, ChannelStreamWriterResolver, ChannelStreamWriterResolverManager, org.apache.sshd.common.Closeable, org.apache.sshd.common.config.keys.FilePasswordProviderHolder, org.apache.sshd.common.config.keys.FilePasswordProviderManager, FactoryManager, PortForwardingEventListenerManager, org.apache.sshd.common.io.IoServiceEventListenerManager, KexExtensionHandlerManager, KexFactoryManager, org.apache.sshd.common.keyprovider.KeyIdentityProviderHolder, org.apache.sshd.common.PropertyResolver, ReservedSessionMessagesManager, SessionDisconnectHandlerManager, org.apache.sshd.common.session.SessionHeartbeatController, SessionListenerManager, UnknownChannelReferenceHandlerManager, org.apache.sshd.common.signature.SignatureFactoriesHolder, org.apache.sshd.common.signature.SignatureFactoriesManager

public class SshClient extends AbstractFactoryManager implements ClientFactoryManager

Entry point for the client side of the SSH protocol.

The default configured client can be created using the setUpDefaultClient(). The next step is to configure and then start the client using the start() method.

Sessions can then be created using on of the ClientSessionCreator.connect(String, String, int) or ClientSessionCreator.connect(String, java.net.SocketAddress) methods.

The client can be stopped any time using the stop() method.

Following is an example of using the SshClient:


try (SshClient client = SshClient.setUpDefaultClient()) {
     ...further configuration of the client...
     client.start();

     try (ClientSession session = client.connect(login, host, port)
                 .verify(...timeout...)
                 .getSession()) {
         session.addPasswordIdentity(password);
         session.auth().verify(...timeout...);

         try (ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL)) {
             channel.setIn(new NoCloseInputStream(System.in));
             channel.setOut(new NoCloseOutputStream(System.out));
             channel.setErr(new NoCloseOutputStream(System.err));
             channel.open();
             channel.waitFor(ClientChannel.CLOSED, 0);
         } finally {
             session.close(false);
         }
   } finally {
       client.stop();
   }
}

Note: the idea is to have one SshClient instance for the entire application and re-use it repeatedly in order to create as many sessions as necessary - possibly with different hosts, ports, users, passwords, etc. - including concurrently. In other words, except for exceptional cases, it is recommended to initialize one instance of SshClient for the application and then use throughout - including for multi-threading. As long as the SshClient is not re-configured it should be multi-thread safe regardless of the target session being created.
Author:
Apache MINA SSHD Project
  • Field Details

    • DEFAULT_SSH_CLIENT_FACTORY

      public static final org.apache.sshd.common.Factory<SshClient> DEFAULT_SSH_CLIENT_FACTORY
    • DEFAULT_USER_AUTH_FACTORIES

      public static final List<UserAuthFactory> DEFAULT_USER_AUTH_FACTORIES
      Default user authentication preferences if not set
      See Also:
    • DEFAULT_SERVICE_FACTORIES

      public static final List<ServiceFactory> DEFAULT_SERVICE_FACTORIES
    • connector

      protected org.apache.sshd.common.io.IoConnector connector
    • sessionFactory

      protected SessionFactory sessionFactory
    • userAuthFactories

      protected List<UserAuthFactory> userAuthFactories
  • Constructor Details

    • SshClient

      public SshClient()
  • Method Details