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.
| Modifier and Type | Field and Description |
|---|---|
| private ClientIdentityLoader | |
| protected IoConnector | |
| public static final List | |
| public static final Factory | |
| public static final List | DEFAULT_USER_AUTH_FACTORIES
Default user authentication preferences if not set |
| private FilePasswordProvider | |
| private HostBasedAuthenticationReporter | |
| private HostConfigEntryResolver | |
| private final List | |
| private final AuthenticationIdentitiesProvider | |
| private KeyIdentityProvider | |
| private PasswordAuthenticationReporter | |
| private PasswordIdentityProvider | |
| private ClientProxyConnector | |
| private PublicKeyAuthenticationReporter | |
| private ServerKeyVerifier | |
| protected SessionFactory | |
| private final AtomicBoolean | |
| protected List | |
| private UserInteraction |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | addPasswordIdentity(String
Password to be added - may not be password)null/emptyNote this password is in
addition to whatever passwords are available via the Implements org.
|
| public void | addPublicKeyIdentity(KeyPair
The kp)KeyPair to add - may not be null Note: this key is in addition to
whatever keys are available via the org. (if
any)Implements org.
|
| protected void | |
| public ConnectFuture | connect(String
The server uri to connect to uriStr)Implements org. Resolves the effective |
| public ConnectFuture | connect(String
The intended username username, SocketAddress The intended target targetAddress, AttributeRepository SocketAddress - never null. If this is an
java. then the effective HostConfigEntry is
resolved and used.An optional "context" to be attached to the established session if successfully
connected context, SocketAddress The local address to use - if localAddress)null an automatic ephemeral port and bind address is
usedImplements org. Resolves the effective |
| public ConnectFuture | connect(String
The intended username username, String The target host name/address - never host, int null/emptyThe 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 localAddress)null an automatic ephemeral port and bind address is
usedImplements org. Resolves the effective |
| public ConnectFuture | connect(HostConfigEntry
The effective hostConfig, AttributeRepository HostConfigEntry to connect to - never nullAn optional "context" to be attached to the established session if successfully
connected context, SocketAddress The local address to use - if localAddress)null an automatic ephemeral port and bind address is
usedImplements org.
|
| protected SshFutureListener | createConnectCompletionListener(ConnectFuture connectFuture, String username, SocketAddress address, KeyIdentityProvider identities, HostConfigEntry hostConfig)
|
| protected IoConnector | |
| protected SessionFactory | |
| protected ConnectFuture | doConnect(HostConfigEntry hostConfig, List<HostConfigEntry> jumps, AttributeRepository context, SocketAddress localAddress)
|
| protected ConnectFuture | doConnect(String username, SocketAddress targetAddress, AttributeRepository context, SocketAddress localAddress, KeyIdentityProvider identities, HostConfigEntry hostConfig)
|
| protected KeyIdentityProvider | ensureFilePasswordProvider(KeyIdentityProvider
KeyIdentityProvider to set upSets this client's |
| public ClientIdentityLoader | |
| public ClientProxyConnector | |
| public FilePasswordProvider | |
| public HostBasedAuthenticationReporter | |
| public HostConfigEntryResolver | |
| protected Closeable | |
| public KeyIdentityProvider | |
| public PasswordAuthenticationReporter | |
| public PasswordIdentityProvider | getPasswordIdentityProvider()
Implements org. Retrieve |
| public PublicKeyAuthenticationReporter | |
| public AuthenticationIdentitiesProvider | |
| public ServerKeyVerifier | getServerKeyVerifier()
Implements org. Retrieve the server key verifier to be used to check the key when connecting to an SSH server. |
| public SessionFactory | |
| public List | getUserAuthFactories()
Implements org. Retrieve the list of named factories for |
| public UserInteraction | |
| public boolean | |
| protected void | onConnectOperationComplete(IoSession ioSession, ConnectFuture connectFuture, String username, SocketAddress address, KeyIdentityProvider identities, HostConfigEntry hostConfig)
|
| public void | |
| protected List | |
| protected List | |
| protected KeyIdentityProvider | |
| public String | removePasswordIdentity(String
The password to remove - ignored if password)null/emptyImplements org.
|
| public KeyPair | removePublicKeyIdentity(KeyPair
The kp)KeyPair to remove - ignored if nullImplements org.
|
| 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 | setKeyIdentityProvider(KeyIdentityProvider keyIdentityProvider)
Implements org.
|
| public static < The generic client class C extends SshClient> C | setKeyPairProvider(C
The client, boolean SshClient to updatedIf strict, boolean true then files that do not have the required access rights are
excluded from considerationIf supportedOnly, FilePasswordProvider true then ignore identities that are not supported internallyA provider, LinkOption... 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 loadedThe options)LinkOptions to apply when checking for existence
|
| public static < The generic client class C extends SshClient> C | setKeyPairProvider(C
The client, Path SshClient to updatedThe folder to scan for the built-in identities dir, boolean If strict, boolean true then files that do not have the required access rights are
excluded from considerationIf supportedOnly, FilePasswordProvider true then ignore identities that are not supported internallyA provider, LinkOption... 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 loadedThe options)LinkOptions to apply when checking for existence
|
| public void | |
| public void | |
| public void | |
| public void | setServerKeyVerifier(ServerKeyVerifier serverKeyVerifier)
Implements org.
|
| public void | |
| public static SshClient | Returns: a newly createSshClient with default configurationsSetup a default client. |
| protected void | |
| public static SimpleClient | |
| public void | setUserAuthFactories(List<UserAuthFactory> userAuthFactories)
Implements org.
|
| public void | setUserInteraction(UserInteraction userInteraction)
Implements org.
|
| public void | |
| public void | |
| public String | |
| public static SimpleClient | wrapAsSimpleClient(SshClient
The client instance - never client)nullNote client must be started before the simple client wrapper is used. Wraps an |