public interface SshClient extends Client
This interface defines the general contract for an SSH client that is compatible for both the SSH1 and SSH2 protocols. This provides general authentication and the opening of sessions. Further features may be available depending upon the version of the SSH server and installed protocol support.
IMPORTANT NOTE: Caution should be applied when writing applications that are to support both protocol versions; there are slight differences in the way that SSH1 and SSH2 handle user sessions. SSH1 only supports a single session channel on each connection where as SSH2 supports multiple sessions. When calling openSessionChannel on this interface with an SSH2 connection, a new session is opened for each call and the variable returned can be used independently of other sessions. For example you can execute multiple commands on the remote host by simply opening a session channel for each command. If the connection is SSH1 then calling openSessionChannel will result in the same session being returned each time. Since you can only execute one command, or start the users shell once on each session, code written to handle the case of an SSH2 connection will fail.
Modifier and Type | Method and Description |
---|---|
void |
addAttribute(String key,
Object attribute)
Add an attribute to this connection object.
|
void |
addListener(SshClientListener listener)
Add a listener to this client.
|
int |
authenticate(SshAuthentication auth)
Authenticate the user.
|
boolean |
cancelRemoteForwarding(String bindAddress,
int bindPort)
Cancel a forwarding request.
|
void |
connect(SshTransport transport,
SshContext context,
SshClientConnector connector,
String username,
String localIdentification,
String remoteIdentification,
boolean buffered)
Connect to an SSH server.
|
void |
disconnect()
Disconnect from the remote computer.
|
SshClient |
duplicate()
Create an identical version of an SshClient using cached authentication
information and the SshTransport duplicate method.
|
Object |
getAttribute(String key)
Get a previously added attribute from this object.
|
<T> T |
getAttribute(String key,
T defaultValue)
Get an attribute or default
|
int |
getChannelCount()
Get the number of active channels.
|
SshContext |
getContext()
Get the context that created this client.
|
String |
getHost()
Public accessor method for getTransport().getHost().
|
String |
getIdent()
The SSH identifier for this host.
|
String |
getRemoteIdentification()
Returns the identification string supplied by the server during protocol
negotiation.
|
SshTransport |
getTransport()
Get the transport this client is running over
|
String |
getUsername()
Return the username used for this connection
|
String |
getUuid() |
int |
getVersion()
Returns the version for this client.
|
boolean |
hasAttribute(String key)
Determine if connection already has an attribute for given key.
|
boolean |
isAuthenticated()
Evaluate whether the user has been authenticated.
|
boolean |
isBuffered()
Identifies whether this client is in buffered mode
|
boolean |
isConnected()
Evaluate whether the connection is still alive.
|
SshTunnel |
openForwardingChannel(String hostname,
int port,
String listeningAddress,
int listeningPort,
String originatingHost,
int originatingPort,
SshTransport transport,
ChannelEventListener listener)
Open a TCPIP forwarding channel to the remote computer.
|
SshClient |
openRemoteClient(String hostname,
int port,
String username)
Open up an SSH client from the remote machine to another remote server.
|
SshClient |
openRemoteClient(String hostname,
int port,
String username,
SshClientConnector con)
Open up an SSH client from the remote machine to another remote server.
|
SshSession |
openSessionChannel()
Open a session on the remote computer.
|
SshSession |
openSessionChannel(ChannelEventListener listener)
Open a session on the remote computer.
|
SshSession |
openSessionChannel(ChannelEventListener listener,
long timeout)
Open a session on the remote computer.
|
SshSession |
openSessionChannel(int windowspace,
int packetsize,
ChannelEventListener listener) |
SshSession |
openSessionChannel(int windowspace,
int packetsize,
ChannelEventListener listener,
long timeout) |
SshSession |
openSessionChannel(long timeout)
Open a session on the remote computer.
|
int |
requestRemoteForwarding(String bindAddress,
int bindPort,
String hostToConnect,
int portToConnect,
ForwardingRequestListener listener)
Requests that the remote computer accepts socket connections and forward
them to the local computer.
|
void |
setIdleConnectionTimeoutSecs(long seconds) |
void connect(SshTransport transport, SshContext context, SshClientConnector connector, String username, String localIdentification, String remoteIdentification, boolean buffered) throws SshException
transport
- the transport layercontext
- an SSH contextusername
- the users namelocalIdentification
- the local identification stringremoteIdentification
- the remotes identification stringbuffered
- should the connection be buffered (threaded)SshException
int authenticate(SshAuthentication auth) throws SshException
Authenticate the user. Once connected call to authenticate the user. When a connection is made no other operations can be performed until the user has been authenticated.
auth
- the authentication mechanism.SshException
SshSession openSessionChannel() throws SshException, ChannelOpenException
Open a session on the remote computer. This can only be called once the user has been authenticated. The session returned is uninitialized and will be opened when either a command is executed or the users shell has been started.
SshException
ChannelOpenException
SshSession openSessionChannel(long timeout) throws SshException, ChannelOpenException
Open a session on the remote computer. This can only be called once the user has been authenticated. The session returned is uninitialized and will be opened when either a command is executed or the users shell has been started.
timeout
- this is an advanced option and should be set to anything other than zero with caution. This is not a socket timeout.SshException
ChannelOpenException
SshSession openSessionChannel(ChannelEventListener listener) throws SshException, ChannelOpenException
Open a session on the remote computer. This can only be called once the user has been authenticated. The session returned is uninitialized and will be opened when either a command is executed or the users shell has been started.
listener
- an event listener to add before openingSshException
ChannelOpenException
SshSession openSessionChannel(ChannelEventListener listener, long timeout) throws SshException, ChannelOpenException
Open a session on the remote computer. This can only be called once the user has been authenticated. The session returned is uninitialized and will be opened when either a command is executed or the users shell has been started.
listener
- an event listener to add before openingtimeout
- SshException
ChannelOpenException
SshSession openSessionChannel(int windowspace, int packetsize, ChannelEventListener listener) throws ChannelOpenException, SshException
windowspace
- packetsize
- listener
- ChannelOpenException
SshException
SshSession openSessionChannel(int windowspace, int packetsize, ChannelEventListener listener, long timeout) throws ChannelOpenException, SshException
windowspace
- packetsize
- listener
- timeout
- ChannelOpenException
SshException
SshTunnel openForwardingChannel(String hostname, int port, String listeningAddress, int listeningPort, String originatingHost, int originatingPort, SshTransport transport, ChannelEventListener listener) throws SshException, ChannelOpenException
Open a TCPIP forwarding channel to the remote computer. If successful the remote computer will open a socket to the host/port specified and return a channel which can be used to forward TCPIP data from the local computer to the remotley connected socket.
It should be noted that this is a low level API method and it does not connect the transport to the channel as this would require some threading. The transport is passed here so that it can be attached to the SshTunnel that is returned. If you want to have the API automatically connect the channel to the transport you should use the ForwardingClient which provides management of forwarding connections and threads.
hostname
- the host to connect toport
- the port to connect tooriginatingHost
- the originating host (informational only)originatingPort
- the originating port (informational only)transport
- listener
- an event listener that will be added to the channel before opening.SshException
ChannelOpenException
SshClient openRemoteClient(String hostname, int port, String username, SshClientConnector con) throws SshException, ChannelOpenException
SshClient
instance to any other machine on the
same network.hostname
- the name of the remote hostport
- the port of the remote hostusername
- the name of the user on the remote hostcon
- an SshConnector
instance that will be used to connect
the client. This does not have to be the same instance that created
this client.SshException
ChannelOpenException
SshClient openRemoteClient(String hostname, int port, String username) throws SshException, ChannelOpenException
SshClient
instance to any other machine on the
same network.hostname
- the name of the remote hostport
- the port of the remote hostusername
- the name of the user on the remote hostSshException
ChannelOpenException
int requestRemoteForwarding(String bindAddress, int bindPort, String hostToConnect, int portToConnect, ForwardingRequestListener listener) throws SshException
bindAddress
- the address that the remote computer should listen onbindPort
- the port that the remote computer should listen onhostToConnect
- the host to connect when a connection is establishedportToConnect
- the port to connect when a connection is establishedlistener
- a callback interfaceSshException
boolean cancelRemoteForwarding(String bindAddress, int bindPort) throws SshException
bindAddress
- the address that the remote computer is listening on.bindPort
- the port that the remote computer is listening on.SshException
void disconnect()
void addListener(SshClientListener listener)
listener
- boolean isAuthenticated()
true
immediatley
after connection. No other operations can be perform until the user has been
authenticated.true
if the connection is authenticated, otherwise false
boolean isConnected()
true
if connected, otherwise false
String getRemoteIdentification()
String getUsername()
SshClient duplicate() throws SshException
SshException
SshContext getContext()
int getChannelCount()
int getVersion()
boolean isBuffered()
SshTransport getTransport()
void addAttribute(String key, Object attribute)
key
- attribute
- Object getAttribute(String key)
key
- <T> T getAttribute(String key, T defaultValue)
key
- defaultValue
- boolean hasAttribute(String key)
key
- String getIdent()
String getHost()
String getUuid()
void setIdleConnectionTimeoutSecs(long seconds)
Copyright © 2024. All rights reserved.