public final class SshConnector extends Object implements SshClientConnector
Connecting to an SSH server STARTS HERE!. This utility class establishes a connection with an SSH server, determines which SSH protocol versions are supported and creates an initialized connection ready for authentication. Both SSH1 and SSH2 protocol support is included, however it is possible to restrict either version by removing the maverick-ssh1.jar or maverick-ssh2.jar from your classpath or by using the setSupportedVersions(int versions) method.
Each call to createInstance() returns a new instance of the connector with a pair of configuration contexts, one for SSH1 and another for SSH2. These contexts are represented by the interface SshContext and have a default set of configuration methods; further protocol specific configuration methods can be obtained by casting the values returned by getContext(int version) into either the Ssh1Context or Ssh2Context implementations.
To connect to an SSH server you need to provide an SshTransport which provides the transport layer communication. In most cases this will be a Socket however since this API is designed for many different platforms, a Socket may not always be available so this simple interface is used to allow you to specify the source IO streams. The SshTransport documentation provides a simple example for a Socket called SocketTransport which is used in the following examples.
To create a connection and authentication using password authentication with default configuration contexts use:
SshConnector con = SshConnector.getInstance(); SshClient ssh = con.connect(new SocketTransport("beagle2.sshtools.net", 22), "martianx"); PasswordAuthentication pwd = new PasswordAuthentication(); pwd.setPassword("likeidgivethataway!"); if (ssh.authenticate(pwd) == SshAuthentication.COMPLETE) { System.out.println("Authentication succeeded"); SshSession sesison = ssh.openSessionChannel(); } else { System.out.println("Authentication failed"); }
To configure host key verification for a connector use the following code:
You can then create as many connections are required using the same configuration contexts.SshConnector con = SshConnector.getInstance(); HostKeyVerification hkv = new HostKeyVerification() { public boolean verifyHost(String name, SshPublicKey key) throws IOException { // Verify the host somehow??? return true; } }; SshContext context = con.getContext(SshConnector.SSH1); context.setHostKeyVerification(hkv); context = con.getContext(SshConnector.SSH2); context.setHostKeyVerification(hkv); SshClient ssh = con.connect( new SocketTransport("beagle2.sshtools.net", 22), "martianx"); .......
To configure protocol specific features, for example the showing of authentication banners from SSH2 servers use:
SshConnector con = SshConnector.getInstance(); Ssh2Context context = (Ssh2Context) con.getContext(SshConnector.SSH2); context.setBannerDisplay(new BannerDisplay() { public void displayBanner(String message) { // Popup a dialog with the message?? } }); SshClient ssh = con.connect( new SocketTransport("beagle2.sshtools.net", 22), "martianx"); ......
Modifier and Type | Field and Description |
---|---|
static Throwable |
initException |
static int |
SSH1
Constant for the SSH protocol version 1
|
static int |
SSH2
Constant for the SSH protocol version 2
|
Modifier and Type | Method and Description |
---|---|
static void |
addEventListener(EventListener listener) |
static void |
addEventListener(String threadPrefix,
EventListener listener) |
void |
addListener(SshClientListener listener) |
SshClient |
connect(SshTransport transport,
String username)
Create a new connection to an SSH server over the specified transport.
|
SshClient |
connect(SshTransport transport,
String username,
boolean buffered)
See
SshClientConnector.connect(SshTransport, String) for full details. |
SshClient |
connect(SshTransport transport,
String username,
boolean buffered,
SshContext context)
See
SshClientConnector.connect(SshTransport, String) for full details. |
SshClient |
connect(SshTransport transport,
String username,
SshContext context)
See
SshClientConnector.connect(SshTransport, String) for full details. |
static SshConnector |
createInstance()
Deprecated.
Use alternative createInstance method that specifies a minimum SecurityLevel for your application and
|
static SshConnector |
createInstance(ExecutorService executorService)
Deprecated.
Use alternative createInstance method that specifies a minimum SecurityLevel for your application
|
static SshConnector |
createInstance(ExecutorService executor,
SecurityLevel securityLevel,
boolean managedSecurity)
Returns an instance of the
SshConnector . |
static SshConnector |
createInstance(ExecutorService executor,
SecurityPolicy securityPolicy) |
static SshConnector |
createInstance(SecurityLevel securityLevel,
boolean managedSecurity) |
static SshConnector |
createInstance(SecurityPolicy securityPolicy) |
static SshConnector |
createManagedInstance(SecurityLevel securityLevel) |
int |
determineVersion(SshTransport transport)
Determine the version of the server connected to the SshTransport.
|
void |
enableFIPSMode()
Deprecated.
This method is out-of-date. Use JCEProvider.initializeDefaultProvider("BCFIPS") passing a FIPS approved JCE provider
in approved mode to restrict algorithms to only those supported by the approved FIPS environment.
|
static String |
getAvailableCiphers() |
static String |
getAvailableCiphers(SecurityLevel securityLevel) |
static String |
getAvailableCompressions() |
static String |
getAvailableKeyExchanges() |
static String |
getAvailableKeyExchanges(SecurityLevel securityLevel) |
static String |
getAvailableMacs() |
static String |
getAvailableMacs(SecurityLevel securityLevel) |
static String |
getAvailablePublicKeys() |
static String |
getAvailablePublicKeys(SecurityLevel securityLevel) |
Ssh2Context |
getContext()
Get the SSH2 context
|
SshContext |
getContext(int version)
Deprecated.
Future version will only support SSH2. Use getContext without parameters.
|
static String |
getDefaultConfiguration(SecurityLevel securityLevel) |
int |
getInitialTimeout()
Get the initial socket timeout for SSH identification.
|
int |
getMaximumRetryAttempts() |
String |
getProduct() |
static Date |
getReleaseDate()
Returns the release date of the current version.
|
int |
getRetryIntervalSeconds() |
int |
getSupportedVersions()
Deprecated.
Future version will only support SSH2
|
static String |
getVersion()
Returns the current version of the API.
|
boolean |
isLicensed()
Indicates whether the API is licensed or not
|
static void |
printDefaultConfiguration() |
static void |
removeEventListener(String threadPrefix) |
void |
removeListener(SshClientListener listener) |
static void |
setCoreThreadPoolSize(int coreThreadPoolSize)
Deprecated.
|
void |
setInitialTimeout(int initialTimeout)
Set the initial timeout for SSH identification.
|
void |
setKnownHosts(HostKeyVerification hkv)
Set the
HostKeyVerification instance for both protocols. |
static void |
setMaximumNumberThreads(int maximumNumberThreads)
Deprecated.
|
void |
setMaximumRetryAttempts(int maximumRetryAttempts) |
void |
setProduct(String product) |
void |
setRetryIntervalSeconds(int retryIntervalSeconds) |
void |
setSoftwareVersionComments(String softwareComments)
Set the software/version/comments field of the SSH identification string
|
void |
setSupportedVersions(int supported)
Deprecated.
Future version will only support SSH2
|
static void |
setThreadTimeout(long threadTimeout)
Deprecated.
|
public static final int SSH1
public static final int SSH2
public static Throwable initException
public void addListener(SshClientListener listener)
public void removeListener(SshClientListener listener)
@Deprecated public static void setCoreThreadPoolSize(int coreThreadPoolSize)
@Deprecated public static void setMaximumNumberThreads(int maximumNumberThreads)
@Deprecated public static void setThreadTimeout(long threadTimeout)
@Deprecated public static SshConnector createInstance() throws SshException
SshException
@Deprecated public static SshConnector createInstance(ExecutorService executorService) throws SshException
SshException
public static SshConnector createManagedInstance(SecurityLevel securityLevel) throws SshException
SshException
public static SshConnector createInstance(SecurityLevel securityLevel, boolean managedSecurity) throws SshException
SshException
public static SshConnector createInstance(ExecutorService executor, SecurityLevel securityLevel, boolean managedSecurity) throws SshException
SshConnector
. Each instance is
initialized with a pair of default contexts.SshException
public static SshConnector createInstance(SecurityPolicy securityPolicy) throws SshException
SshException
public static SshConnector createInstance(ExecutorService executor, SecurityPolicy securityPolicy) throws SshException
SshException
public static void addEventListener(EventListener listener)
public static void addEventListener(String threadPrefix, EventListener listener)
public static void removeEventListener(String threadPrefix)
@Deprecated public final void enableFIPSMode() throws SshException
SshException
public final boolean isLicensed()
@Deprecated public SshContext getContext(int version) throws SshException
Get the configuration context for a specific SSH version.
orSshConnector con = SshConnector.getInstance(); SshContext context = con.getContext(SshConnector.SSH1);
You can cast the returned value into the context implementation to get access to protocol specific configuration.SshContext context = con.getContext(SshConnector.SSH2);
Ssh2Context context = (Ssh2Context) con.getContext(SshConnector.SSH2);
version
- either SshConnector.SSH1 or SshConnector.SSH2SshException
public Ssh2Context getContext() throws SshException
SshException
@Deprecated public void setSupportedVersions(int supported) throws SshException
Sets the protocol versions supported by this connector. When the connector is created, the supported flags are set to whichever protocol support is installed. You can restrict support by setting the support to an individual version using this method. For example you could restrict access to SSH2 by using the following code:
or switch back to full support:SshConnector con = SshConnector.getInstance(); con.setSupportedVersions(SshConnector.SSH2);
con.setSupportedVersions(SshConnector.SSH1 | SshConnector.SSH2);
supported
- SshException
@Deprecated public int getSupportedVersions()
public void setKnownHosts(HostKeyVerification hkv)
HostKeyVerification
instance for both protocols. You can
use this method to configure both protocols at once instead of using
seperate calls on each of the SshContext
's.hkv
- public SshClient connect(SshTransport transport, String username) throws SshException
SshClientConnector
Create a new connection to an SSH server over the specified transport.
This method reads the remote servers identification and determines which
protocol support is required. An SshClient
instance is then
created, initialized and returned. If both protocol versions are
supported by the remote server the connector will always operate using
SSH2.
The SshTransport
interface is used here to allow different types
of transport mechanisms. Typically this would be a Socket however since
this API is targeted at all Java platforms a Socket cannot be used
directly. See the SshTransport
documentation for an example
Socket implementation.
To take advantage of protocol specific features you can test the instance
returned which will either be an Ssh1Client
or
Ssh2Client
.
connect
in interface SshClientConnector
transport
- the transport for the connection.username
- the name of the user connectingSshException
Ssh1Client
,
Ssh2Client
public SshClient connect(SshTransport transport, String username, boolean buffered) throws SshException
SshClientConnector
SshClientConnector.connect(SshTransport, String)
for full details. This method
optionally allows you to specify the buffered state of the connection.
When the connection is buffered a background thread is started to act as
a message pump; this has the benefit of routing data as soon as it
arrives and helps in circumstances where you require a channel to fill up
with data without calling its InputStream's read method. This also will
enable the InputStreams available method to work as expected.connect
in interface SshClientConnector
transport
- SshTransportusername
- Stringbuffered
- booleanSshException
public SshClient connect(SshTransport transport, String username, SshContext context) throws SshException
SshClientConnector
SshClientConnector.connect(SshTransport, String)
for full details. This method
optionally allows you to specify a context to use. Normally you would
reused an SshConnector
instead of calling this method directly.connect
in interface SshClientConnector
transport
- SshTransportusername
- Stringcontext
- SshContextSshException
public void setSoftwareVersionComments(String softwareComments)
softwareComments
- Stringpublic SshClient connect(SshTransport transport, String username, boolean buffered, SshContext context) throws SshException
SshClientConnector
SshClientConnector.connect(SshTransport, String)
for full details.
This method optionally allows you to specify the buffered state of the connection. When the connection is buffered a background thread is started to act as a message pump; this has the benefit of routing data as soon as it arrives and helps in circumstances where you require a channel to fill up with data without calling its InputStream's read method. This also will enable the InputStreams available method to work as expected.
This method also allows you to specify a context to use. Normally you
would reuse an SshConnector
instead of calling this method
directly.
connect
in interface SshClientConnector
transport
- SshTransportusername
- Stringbuffered
- booleancontext
- SshContextSshException
public static String getVersion()
public static Date getReleaseDate()
public int determineVersion(SshTransport transport) throws SshException
transport
- SshException
public int getInitialTimeout()
public void setInitialTimeout(int initialTimeout)
initialTimeout
- the number of milliseconds to use as the socket timeout.public String getProduct()
public void setProduct(String product)
public int getMaximumRetryAttempts()
public void setMaximumRetryAttempts(int maximumRetryAttempts)
public int getRetryIntervalSeconds()
public void setRetryIntervalSeconds(int retryIntervalSeconds)
public static String getAvailableCiphers(SecurityLevel securityLevel) throws SshException
SshException
public static String getAvailableMacs(SecurityLevel securityLevel) throws SshException
SshException
public static String getAvailablePublicKeys(SecurityLevel securityLevel) throws SshException
SshException
public static String getAvailableKeyExchanges(SecurityLevel securityLevel) throws SshException
SshException
public static void printDefaultConfiguration() throws SshException
SshException
public static String getAvailableCompressions() throws SshException
SshException
public static String getAvailableMacs() throws SshException
SshException
public static String getAvailableCiphers() throws SshException
SshException
public static String getAvailableKeyExchanges() throws SshException
SshException
public static String getAvailablePublicKeys() throws SshException
SshException
public static String getDefaultConfiguration(SecurityLevel securityLevel) throws SshException
SshException
Copyright © 2024. All rights reserved.