Class SshClient

All Implemented Interfaces:
Closeable, AutoCloseable, Channel, ClientAuthenticationManager, ClientFactoryManager, ClientIdentityLoaderHolder, ClientIdentityLoaderManager, ClientProxyConnectorHolder, ClientSessionCreator, AttributeRepository, AttributeStore, UserAuthFactoriesManager<ClientSession,UserAuth,UserAuthFactory>, ChannelListenerManager, ChannelStreamWriterResolver, ChannelStreamWriterResolverManager, Closeable, FilePasswordProviderHolder, FilePasswordProviderManager, FactoryManager, PortForwardingEventListenerManager, IoServiceEventListenerManager, KexExtensionHandlerManager, KexFactoryManager, KeyIdentityProviderHolder, PropertyResolver, ReservedSessionMessagesManager, SessionDisconnectHandlerManager, SessionHeartbeatController, SessionListenerManager, UnknownChannelReferenceHandlerManager, SignatureFactoriesHolder, SignatureFactoriesManager

public class SshClient extends AbstractFactoryManager implements ClientFactoryManager, Closeable

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