Top Description Fields Constructors Methods
org.apache.sshd.client

public Class SshClient

extends AbstractFactoryManager
implements ClientFactoryManager
Class Inheritance
All Implemented Interfaces
org.apache.sshd.client.ClientFactoryManager, org.apache.sshd.client.ClientAuthenticationManager, org.apache.sshd.common.keyprovider.KeyIdentityProviderHolder, org.apache.sshd.common.auth.UserAuthFactoriesManager, org.apache.sshd.client.config.keys.ClientIdentityLoaderManager, org.apache.sshd.client.config.keys.ClientIdentityLoaderHolder, org.apache.sshd.common.config.keys.FilePasswordProviderManager, org.apache.sshd.common.config.keys.FilePasswordProviderHolder, org.apache.sshd.client.session.ClientProxyConnectorHolder, org.apache.sshd.client.session.ClientSessionCreator, org.apache.sshd.common.FactoryManager, org.apache.sshd.common.session.SessionHeartbeatController, org.apache.sshd.common.PropertyResolver, org.apache.sshd.common.AttributeStore, org.apache.sshd.common.AttributeRepository, org.apache.sshd.common.io.IoServiceEventListenerManager, org.apache.sshd.common.forward.PortForwardingEventListenerManager, org.apache.sshd.common.session.UnknownChannelReferenceHandlerManager, org.apache.sshd.common.channel.throttle.ChannelStreamWriterResolverManager, org.apache.sshd.common.channel.throttle.ChannelStreamWriterResolver, org.apache.sshd.common.channel.ChannelListenerManager, org.apache.sshd.common.session.SessionDisconnectHandlerManager, org.apache.sshd.common.session.ReservedSessionMessagesManager, org.apache.sshd.common.session.SessionListenerManager, org.apache.sshd.common.kex.KexFactoryManager, org.apache.sshd.common.kex.extension.KexExtensionHandlerManager, org.apache.sshd.common.signature.SignatureFactoriesManager, org.apache.sshd.common.signature.SignatureFactoriesHolder
Imports
java.io.IOException, java.net.InetSocketAddress, .SocketAddress, .SocketTimeoutException, .URI, java.nio.channels.UnsupportedAddressTypeException, java.nio.file.LinkOption, .Path, .Paths, java.security.GeneralSecurityException, .KeyPair, java.time.Duration, java.util.ArrayList, .Arrays, .Collection, .Collections, .Iterator, .List, .Objects, java.util.concurrent.CopyOnWriteArrayList, java.util.concurrent.atomic.AtomicBoolean, .AtomicReference, java.util.stream.Collectors, org.apache.sshd.agent.SshAgentFactory, org.apache.sshd.client.auth.AuthenticationIdentitiesProvider, .UserAuthFactory, org.apache.sshd.client.auth.hostbased.HostBasedAuthenticationReporter, org.apache.sshd.client.auth.keyboard.UserAuthKeyboardInteractiveFactory, .UserInteraction, org.apache.sshd.client.auth.password.PasswordAuthenticationReporter, .PasswordIdentityProvider, .UserAuthPasswordFactory, org.apache.sshd.client.auth.pubkey.PublicKeyAuthenticationReporter, .UserAuthPublicKey, .UserAuthPublicKeyFactory, org.apache.sshd.client.config.hosts.HostConfigEntry, .HostConfigEntryResolver, org.apache.sshd.client.config.keys.ClientIdentity, .ClientIdentityLoader, .DefaultClientIdentitiesWatcher, org.apache.sshd.client.future.AuthFuture, .ConnectFuture, .DefaultConnectFuture, org.apache.sshd.client.keyverifier.ServerKeyVerifier, org.apache.sshd.client.session.AbstractClientSession, .ClientConnectionServiceFactory, .ClientProxyConnector, .ClientSession, .ClientUserAuthServiceFactory, .SessionFactory, org.apache.sshd.client.session.forward.ExplicitPortForwardingTracker, org.apache.sshd.client.simple.AbstractSimpleClientSessionCreator, .SimpleClient, org.apache.sshd.common.AttributeRepository, .Closeable, .Factory, .NamedResource, .ServiceFactory, org.apache.sshd.common.channel.ChannelFactory, org.apache.sshd.common.config.keys.FilePasswordProvider, .FilePasswordProviderManager, .KeyUtils, .PublicKeyEntry, org.apache.sshd.common.future.CancelFuture, .Cancellable, .SshFutureListener, org.apache.sshd.common.helpers.AbstractFactoryManager, org.apache.sshd.common.io.IoConnectFuture, .IoConnector, .IoSession, org.apache.sshd.common.keyprovider.AbstractResourceKeyPairProvider, .KeyIdentityProvider, .KeyPairProvider, .MultiKeyIdentityProvider, org.apache.sshd.common.session.helpers.AbstractSession, org.apache.sshd.common.util.ExceptionUtils, .GenericUtils, .ValidateUtils, org.apache.sshd.common.util.functors.UnaryEquator, org.apache.sshd.common.util.io.resource.PathResource, org.apache.sshd.common.util.net.SshdSocketAddress, org.apache.sshd.core.CoreModuleProperties

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 connect(String, String, int) or 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 Summary

Modifier and TypeField and Description
private ClientIdentityLoader
protected IoConnector
public static final List<ServiceFactory>
public static final Factory<SshClient>
public static final List<UserAuthFactory>
DEFAULT_USER_AUTH_FACTORIES

Default user authentication preferences if not set

private FilePasswordProvider
private HostBasedAuthenticationReporter
private HostConfigEntryResolver
private final List<Object>
private final AuthenticationIdentitiesProvider
private KeyIdentityProvider
private PasswordAuthenticationReporter
private PasswordIdentityProvider
private ClientProxyConnector
private PublicKeyAuthenticationReporter
private ServerKeyVerifier
protected SessionFactory
private final AtomicBoolean
protected List<UserAuthFactory>
private UserInteraction
Inherited from org.apache.sshd.common.helpers.AbstractFactoryManager:
agentFactorychannelFactorieschannelListenerProxychannelListenersexecutorfileSystemFactoryforwarderFactoryforwardingFilterglobalRequestHandlersioServiceFactoryioServiceFactoryFactoryrandomFactoryserviceFactoriessessionListenerProxysessionListenerssessionTimeoutListenershutdownExecutortimeoutListenerFuturetunnelListenerProxytunnelListeners

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addPasswordIdentity(String
Password to be added - may not be null/empty

Note

this password is in addition to whatever passwords are available via the PasswordIdentityProvider (if any)

password
)

Implements org.apache.sshd.client.ClientAuthenticationManager.addPasswordIdentity.

public void
addPublicKeyIdentity(KeyPair
The KeyPair to add - may not be null Note: this key is in addition to whatever keys are available via the org.apache.sshd.common.keyprovider.KeyIdentityProvider (if any)
kp
)

Implements org.apache.sshd.client.ClientAuthenticationManager.addPublicKeyIdentity.

protected void
public ConnectFuture
connect(String
The server uri to connect to
uriStr
)

Implements org.apache.sshd.client.session.ClientSessionCreator.connect.

Resolves the effective HostConfigEntry and connects to it

public ConnectFuture
connect(String
The intended username
username
,
SocketAddress
The intended target SocketAddress - never null. If this is an java.net.InetSocketAddress then the effective HostConfigEntry is resolved and used.
targetAddress
,
AttributeRepository
An optional "context" to be attached to the established session if successfully connected
context
,
SocketAddress
The local address to use - if null an automatic ephemeral port and bind address is used
localAddress
)

Implements org.apache.sshd.client.session.ClientSessionCreator.connect.

Resolves the effective HostConfigEntry and connects to it

public ConnectFuture
connect(String
The intended username
username
,
String
The target host name/address - never null/empty
host
,
int
The target port
port
,
AttributeRepository
An optional "context" to be attached to the established session if successfully connected
context
,
SocketAddress
The local address to use - if null an automatic ephemeral port and bind address is used
localAddress
)

Implements org.apache.sshd.client.session.ClientSessionCreator.connect.

Resolves the effective HostConfigEntry and connects to it

public ConnectFuture
connect(HostConfigEntry
The effective HostConfigEntry to connect to - never null
hostConfig
,
AttributeRepository
An optional "context" to be attached to the established session if successfully connected
context
,
SocketAddress
The local address to use - if null an automatic ephemeral port and bind address is used
localAddress
)

Implements org.apache.sshd.client.session.ClientSessionCreator.connect.

protected SshFutureListener<IoConnectFuture>
protected IoConnector
protected SessionFactory
protected ConnectFuture
protected ConnectFuture
doConnect(String username, SocketAddress targetAddress, AttributeRepository context, SocketAddress localAddress, KeyIdentityProvider identities, HostConfigEntry hostConfig)

protected KeyIdentityProvider

Returns:

a KeyIdentityProvider configured with a FilePasswordProvider
ensureFilePasswordProvider
(KeyIdentityProvider identities)

Sets this client's FilePasswordProvider on the KeyIdentityProvider if it is an AbstractResourceKeyPairProvider or implements FilePasswordProviderManager and doesn't have one yet.

public ClientIdentityLoader
public ClientProxyConnector
public FilePasswordProvider
public HostBasedAuthenticationReporter
public HostConfigEntryResolver
protected Closeable
public KeyIdentityProvider
public PasswordAuthenticationReporter
public PasswordIdentityProvider
public PublicKeyAuthenticationReporter
public AuthenticationIdentitiesProvider
public ServerKeyVerifier
getServerKeyVerifier()

Implements org.apache.sshd.client.ClientAuthenticationManager.getServerKeyVerifier.

Retrieve the server key verifier to be used to check the key when connecting to an SSH server.

public SessionFactory
public List<UserAuthFactory>
getUserAuthFactories()

Implements org.apache.sshd.common.auth.UserAuthFactoriesManager.getUserAuthFactories.

Retrieve the list of named factories for UserAuth objects.

public UserInteraction
public boolean
protected void
onConnectOperationComplete(IoSession ioSession, ConnectFuture connectFuture, String username, SocketAddress address, KeyIdentityProvider identities, HostConfigEntry hostConfig)

public void
open()

protected List<HostConfigEntry>
protected List<HostConfigEntry>
protected KeyIdentityProvider
public String
removePasswordIdentity(String
The password to remove - ignored if null/empty
password
)

Implements org.apache.sshd.client.ClientAuthenticationManager.removePasswordIdentity.

public KeyPair
protected HostConfigEntry
resolveHost(String username, String host, int port, AttributeRepository context, SocketAddress localAddress)

public void
public void
public void
public void
public void
public void
public static <
The generic client class
C extends SshClient
>
C

Returns:

The updated client instance - provided a non-null KeyPairProvider was generated
setKeyPairProvider
(C
The SshClient to updated
client
,
boolean
If true then files that do not have the required access rights are excluded from consideration
strict
,
boolean
If true then ignore identities that are not supported internally
supportedOnly
,
FilePasswordProvider
A FilePasswordProvider - may be null if the loaded keys are guaranteed not to be encrypted. The argument to FilePasswordProvider#getPassword is the path of the file whose key is to be loaded
provider
,
LinkOption...
The LinkOptions to apply when checking for existence
options
)

public static <
The generic client class
C extends SshClient
>
C

Returns:

The updated client instance - provided a non-null KeyIdentityProvider was generated
setKeyPairProvider
(C
The SshClient to updated
client
,
Path
The folder to scan for the built-in identities
dir
,
boolean
If true then files that do not have the required access rights are excluded from consideration
strict
,
boolean
If true then ignore identities that are not supported internally
supportedOnly
,
FilePasswordProvider
A FilePasswordProvider - may be null if the loaded keys are guaranteed not to be encrypted. The argument to FilePasswordProvider#getPassword is the path of the file whose key is to be loaded
provider
,
LinkOption...
The LinkOptions to apply when checking for existence
options
)

public void
public void
public void
public void
public void
public static SshClient

Returns:

a newly create SshClient with default configurations
setUpDefaultClient
()

Setup a default client.

protected void
public static SimpleClient

Returns:

The SimpleClient wrapper

Note

when the wrapper is closed the client is also stopped

setUpDefaultSimpleClient
()

Setup a default client, starts it and then wraps it as a SimpleClient

public void
public void
public void
start()

Starts the SSH client and can start creating sessions using it.

public void
stop()

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public static SimpleClient

Returns:

The SimpleClient wrapper

Note

when the wrapper is closed the client is also stopped

wrapAsSimpleClient
(SshClient
The client instance - never null

Note

client must be started before the simple client wrapper is used.

client
)

Wraps an SshClient instance as a SimpleClient

Inherited from org.apache.sshd.common.helpers.AbstractFactoryManager:
addChannelListeneraddPortForwardingEventListeneraddSessionListenerattributeKeysclearAttributescomputeAttributeIfAbsentcreateSessionTimeoutListenergetAgentFactorygetAttributegetAttributesCountgetChannelFactoriesgetChannelListenerProxygetChannelStreamWriterResolvergetFileSystemFactorygetForwarderFactorygetForwardingFiltergetGlobalRequestHandlersgetIoServiceEventListenergetIoServiceFactorygetIoServiceFactoryFactorygetNioWorkersgetParentPropertyResolvergetPortForwardingEventListenerProxygetPropertiesgetRandomFactorygetReservedSessionMessagesHandlergetScheduledExecutorServicegetServiceFactoriesgetSessionDisconnectHandlergetSessionListenerProxygetUnknownChannelReferenceHandlergetVersionremoveAttributeremoveChannelListenerremovePortForwardingEventListenerremoveSessionListenerremoveSessionTimeoutresolveUnknownChannelReferenceHandlersetAgentFactorysetAttributesetChannelFactoriessetChannelStreamWriterResolversetFileSystemFactorysetForwarderFactorysetForwardingFiltersetGlobalRequestHandlerssetIoServiceEventListenersetIoServiceFactoryFactorysetNioWorkerssetParentPropertyResolversetRandomFactorysetReservedSessionMessagesHandlersetScheduledExecutorServicesetScheduledExecutorServicesetServiceFactoriessetSessionDisconnectHandlersetUnknownChannelReferenceHandlersetupSessionTimeoutstopSessionTimeoutListener