| java.lang.Object | ||
| ↳ | net.schmizz.sshj.SocketClient | |
| ↳ | net.schmizz.sshj.SSHClient | |
Secure SHell client API.
Before connection is established, host key verification needs to be accounted for. This is done byspecifying one or more HostKeyVerifier objects. Database of known
hostname-key pairs in the OpenSSH "known_hosts" format can be loaded for host
key verification.
User authentication can be performed by any of the auth*() method.
startSession() caters to the most typical use case of starting a session channel and executing a
remote command, starting a subsystem, etc. If you wish to request X11 forwarding for some session, first register a ConnectListener for x11 channels.
Local and remote port forwarding is possible. There
are also utility method for easily creating SCP and SFTP
implementations.
A simple example:
final SSHClient client = new SSHClient();
client.loadKnownHosts();
client.connect("hostname");
try {
client.authPassword("username", "password");
final Session session = client.startSession();
try {
final Command cmd = session.exec("true");
cmd.join(1, TimeUnit.SECONDS);
} finally {
session.close();
}
} finally {
client.disconnect();
}
Where a password or passphrase is required, if you're extra-paranoid use the char[] based method. The char[] will be blanked out after use.
| Constants | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| int | DEFAULT_PORT | Default port for SSH | |||||||||
|
[Expand]
Inherited Constants | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
From class
net.schmizz.sshj.SocketClient
| |||||||||||
| Fields | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| auth | ssh-userauth service |
||||||||||
| conn | ssh-connection service |
||||||||||
| log | Logger | ||||||||||
| trans | Transport layer | ||||||||||
|
[Expand]
Inherited Fields | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
From class
net.schmizz.sshj.SocketClient
| |||||||||||
| Public Constructors | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
Default constructor.
| |||||||||||
Constructor that allows specifying a
config to be used. | |||||||||||
| Public Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
Add a
HostKeyVerifier which will be invoked for verifying host key during connection establishment and
future key exchanges. | |||||||||||
Add a
HostKeyVerifier that will verify any host that's able to claim a host key with the given fingerprint, e.g. | |||||||||||
Authenticate
username using the supplied methods. | |||||||||||
Authenticate
username using the supplied methods. | |||||||||||
Authenticate
username using the "password" authentication method and as a fallback basic
challenge-response authentication.. | |||||||||||
Authenticate
username using the "password" authentication method and as a fallback basic
challenge-response authentication. | |||||||||||
Authenticate
username using the "password" authentication method and as a fallback basic
challenge-response authentication. | |||||||||||
Authenticate
username using the "publickey" authentication method, with keys from one or more
locations in the file system. | |||||||||||
Authenticate
username using the "publickey" authentication method. | |||||||||||
Authenticate
username using the "publickey" authentication method. | |||||||||||
Authenticate
username using the "publickey" authentication method, with keys from some common
locations on the file system. | |||||||||||
Same as
disconnect(). | |||||||||||
Disconnects from the connected SSH server.
| |||||||||||
Returns a
KeyProvider instance created from a location on the file system where an unencrypted
private key file (does not require a passphrase) can be found. | |||||||||||
Creates a
KeyProvider instance from passed strings. | |||||||||||
Convenience method for creating a
KeyProvider instance from a location where an encrypted
key file is located. | |||||||||||
Creates a
KeyProvider instance from given location on the file system. | |||||||||||
Utility function for createing a
KeyProvider instance from given location on the file system. | |||||||||||
Creates a
KeyProvider from supplied KeyPair. | |||||||||||
Adds a
OpenSSHKnownHosts object created from the specified location as a host key verifier. | |||||||||||
Attempts loading the user's
known_hosts file from the default locations, i.e. | |||||||||||
Create a
LocalPortForwarder that will listen based on parameters using the bound
serverSocket and forward incoming connections to the server; which will further forward them to
host:port. | |||||||||||
Register a
listener for handling forwarded X11 channels. | |||||||||||
Does key re-exchange.
| |||||||||||
Opens a
session channel. | |||||||||||
Adds
zlib compression to preferred compression algorithms. | |||||||||||
| Protected Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
Do key exchange.
| |||||||||||
On connection establishment, also initializes the SSH transport via
init(String, int, InputStream, OutputStream) and doKex(). | |||||||||||
|
[Expand]
Inherited Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
From class
net.schmizz.sshj.SocketClient
| |||||||||||
From class
java.lang.Object
| |||||||||||
From interface
java.io.Closeable
| |||||||||||
From interface
net.schmizz.sshj.connection.channel.direct.SessionFactory
| |||||||||||
Default port for SSH
Logger
Constructor that allows specifying a config to be used.
| config | Config instance
|
|---|
Add a HostKeyVerifier which will be invoked for verifying host key during connection establishment and
future key exchanges.
| hostKeyVerifier | HostKeyVerifier instance
|
|---|
Add a HostKeyVerifier that will verify any host that's able to claim a host key with the given fingerprint, e.g. "4b:69:6c:72:6f:79:20:77:61:73:20:68:65:72:65:21"
| fingerprint | Expected fingerprint in colon-delimited format (16 octets in hex delimited by a colon) |
|---|
Authenticate username using the supplied methods.
| username | User to authenticate |
|---|---|
| methods | One or more authentication method |
| UserAuthException | in case of authentication failure |
|---|---|
| TransportException | if there was a transport-layer error |
Authenticate username using the supplied methods.
| username | User to authenticate |
|---|---|
| methods | One or more authentication method |
| UserAuthException | in case of authentication failure |
|---|---|
| TransportException | if there was a transport-layer error |
Authenticate username using the "password" authentication method and as a fallback basic
challenge-response authentication.. The password array is blanked out after use.
| username | User to authenticate |
|---|---|
| password | The password to use for authentication |
| UserAuthException | in case of authentication failure |
|---|---|
| TransportException | if there was a transport-layer error |
Authenticate username using the "password" authentication method and as a fallback basic
challenge-response authentication.
| username | User to authenticate |
|---|---|
| pfinder | The PasswordFinder to use for authentication |
| UserAuthException | in case of authentication failure |
|---|---|
| TransportException | if there was a transport-layer error |
Authenticate username using the "password" authentication method and as a fallback basic
challenge-response authentication.
| username | User to authenticate |
|---|---|
| password | The password to use for authentication |
| UserAuthException | in case of authentication failure |
|---|---|
| TransportException | if there was a transport-layer error |
Authenticate username using the "publickey" authentication method, with keys from one or more
locations in the file system.
locations are specified; authentication is attempted in order as long as the "publickey" authentication method is available. If there is an error loading keys from any of them (e.g. file
could not be read, file format not recognized) that key file it is ignored.
This method does not provide a way to specify a passphrase.| username | User to authenticate |
|---|---|
| locations | One or more locations in the file system containing the private key |
| UserAuthException | in case of authentication failure |
|---|---|
| TransportException | if there was a transport-layer error |
Authenticate username using the "publickey" authentication method.
KeyProvider instances can be created using any of the of the loadKeys() method provided in this
class. In case multiple keyProviders are specified; authentication is attempted in order as long as the
"publickey" authentication method is available.| username | User to authenticate |
|---|---|
| keyProviders | One or more KeyProvider instances |
| UserAuthException | in case of authentication failure |
|---|---|
| TransportException | if there was a transport-layer error |
Authenticate username using the "publickey" authentication method.
KeyProvider instances can be created using any of the loadKeys() method provided in this class.
In case multiple keyProviders are specified; authentication is attempted in order as long as the "publickey" authentication method is available.| username | User to authenticate |
|---|---|
| keyProviders | One or more KeyProvider instances |
| UserAuthException | in case of authentication failure |
|---|---|
| TransportException | if there was a transport-layer error |
Authenticate username using the "publickey" authentication method, with keys from some common
locations on the file system. This method relies on ~/.ssh/id_rsa and ~/.ssh/id_dsa.
| username | User to authenticate |
|---|
| UserAuthException | in case of authentication failure |
|---|---|
| TransportException | if there was a transport-layer error |
Disconnects from the connected SSH server. SSHClient objects are not reusable therefore it is incorrect
to attempt connection after this method has been called.
finally construct after connection is established; so that proper
cleanup is done and the thread spawned by the transport layer for dealing with incoming packets is stopped.
| IOException |
|---|
RemotePortForwarder that allows requesting remote forwarding over this connection. UserAuth instance. This allows access to information like the authentication banner, whether authentication was at least partially successful, and any saved
exceptions that were ignored because there were more authentication method that could be tried.
Returns a KeyProvider instance created from a location on the file system where an unencrypted
private key file (does not require a passphrase) can be found. Simply calls loadKeys(String, PasswordFinder) with the PasswordFinder argument as null.
| location | The location for the key file |
|---|
| SSHException | if there was no suitable key provider available for the file format; typically because BouncyCastle is not in the classpath |
|---|---|
| IOException | if the key file format is not known, if the file could not be read, etc. |
Creates a KeyProvider instance from passed strings. Currently only PKCS8 format private key files are
supported (OpenSSH uses this format).
| privateKey | The private key as a string |
|---|---|
| publicKey | The public key as a string if it's not included with the private key |
| passwordFinder | The PasswordFinder that can supply the passphrase for decryption (may be null in case keyfile is not encrypted) |
| SSHException | if there was no suitable key provider available for the file format; typically because BouncyCastle is not in the classpath |
|---|---|
| IOException | if the key file format is not known, etc. |
Convenience method for creating a KeyProvider instance from a location where an encrypted
key file is located. Calls loadKeys(String, char[]) with a character array created from the supplied
passphrase string.
| location | Location of the key file |
|---|---|
| passphrase | Passphrase as a string |
| IOException | if the key file format is not known, if the file could not be read etc. |
|---|
Creates a KeyProvider instance from given location on the file system. Currently only PKCS8 format
private key files are supported (OpenSSH uses this format).
| location | The location of the key file |
|---|---|
| passwordFinder | The PasswordFinder that can supply the passphrase for decryption (may be null in case keyfile is not encrypted) |
| SSHException | if there was no suitable key provider available for the file format; typically because BouncyCastle is not in the classpath |
|---|---|
| IOException | if the key file format is not known, if the file could not be read, etc. |
Utility function for createing a KeyProvider instance from given location on the file system. Creates a
one-off PasswordFinder using createOneOff(char[]), and calls loadKeys(String, PasswordFinder).
| location | Location of the key file |
|---|---|
| passphrase | Passphrase as a char-array |
| SSHException | if there was no suitable key provider available for the file format; typically because BouncyCastle is not in the classpath |
|---|---|
| IOException | if the key file format is not known, if the file could not be read, etc. |
Creates a KeyProvider from supplied KeyPair.
| kp | The key pair |
|---|
Adds a OpenSSHKnownHosts object created from the specified location as a host key verifier.
| location | Location for known_hosts file |
|---|
| IOException | if there is an error loading from any of these locations |
|---|
Attempts loading the user's known_hosts file from the default locations, i.e. ~/.ssh/known_hosts
and ~/.ssh/known_hosts2 on most platforms. Adds the resulting OpenSSHKnownHosts object as a host
key verifier.
loadKnownHosts(File).| IOException | if there is an error loading from both locations |
|---|
Create a LocalPortForwarder that will listen based on parameters using the bound
serverSocket and forward incoming connections to the server; which will further forward them to
host:port.
listen() method should be called to actually start
listening, this method just creates an instance.| parameters | Parameters for the forwarding setup |
|---|---|
| serverSocket | Bound server socket |
SFTPClient implementation.| IOException | if there is an error starting the sftp subsystem |
|---|
Register a listener for handling forwarded X11 channels. Without having done this, an incoming X11
forwarding will be summarily rejected.
listener.| listener | The ConnectListener that should be delegated the responsibility of handling forwarded
X11Forwarder.X11Channel 's |
|---|
X11Forwarder that allows to stop acting on X11 requests from
server
Does key re-exchange.
| TransportException | if an error occurs during key exchange |
|---|
Opens a session channel. The returned Session instance allows executing a remote command, starting a subsystem, or starting a shell.
session channelAdds zlib compression to preferred compression algorithms. There is no guarantee that it will be
successfully negotiatied.
| ClassNotFoundException | if JZlib is not in classpath |
|---|---|
| TransportException | if an error occurs during renegotiation |
On connection establishment, also initializes the SSH transport via init(String, int, InputStream, OutputStream) and doKex().
| IOException |
|---|