com.mindbright.ssh
Class SSHClient

java.lang.Object
  extended by com.mindbright.ssh.SSH
      extended by com.mindbright.ssh.SSHClient
All Implemented Interfaces:
NIOCallback
Direct Known Subclasses:
SSHInteractiveClient

public class SSHClient
extends SSH
implements NIOCallback

This class contains the main functionality for setting up a connection to a ssh-server. It can be used both to implement a "full" ssh-client, or it can be used to fire off a single command on the server (both in a background thread and in the current-/foreground-thread). A set of properties can be used to control different aspects of the connection. These are fetched from an object implementing the SSHClientUser-interface. The authentication can be done in different ways, all which is handled through an object implementing the SSHAuthenticator-interface. The console-output of the SSHClient is (optionally) handled through an object implementing the SSHConsole-interface.

A class realizing a full interactive ssh-client is SSHInteractiveClient.

See Also:
SSHAuthenticator, SSHClientUser, SSHConsole

Nested Class Summary
static class SSHClient.AuthFailException
           
static class SSHClient.ExitMonitor
           
static class SSHClient.LocalForward
           
static class SSHClient.RemoteForward
           
 
Field Summary
protected  boolean activateTunnels
           
protected  SSHAuthenticator authenticator
           
protected  java.lang.String commandLine
           
protected  SSHConsole console
           
protected  SSHChannelController controller
           
protected  java.lang.Object controllerMonitor
           
protected  java.lang.Integer exitStatus
          Exit code.
protected  int firstFTPPort
           
protected  boolean gracefulExit
           
protected  boolean havePORTFtp
           
protected  com.mindbright.ssh.SSHClient.KeepAliveThread heartbeat
           
protected  SSHInteractor interactor
           
protected  boolean isConnected
           
protected  boolean isOpened
           
 boolean isReading
          SSHClient will set this variable to true when it is reading from the input connection.
protected  java.net.InetAddress localAddr
           
protected  java.util.Vector<SSHClient.LocalForward> localForwards
           
protected  SecureRandomAndPad rand
           
protected  int refCount
           
protected  java.util.Vector<SSHClient.RemoteForward> remoteForwards
           
protected  java.net.InetAddress serverAddr
           
protected  java.net.InetAddress serverRealAddr
           
protected  int srvVersionMajor
           
protected  int srvVersionMinor
           
protected  java.lang.String srvVersionStr
           
protected  NonBlockingInput sshIn
           
protected  NonBlockingOutput sshOut
           
protected  NetworkConnection sshSocket
           
protected  SSHClientUser user
           
 
Fields inherited from class com.mindbright.ssh.SSH
AUTH_CRYPTOCARD, AUTH_DEFAULT, AUTH_GSSAPI, AUTH_HOSTBASED, AUTH_KBDINTERACT, AUTH_KERBEROS, AUTH_NOTSUPPORTED, AUTH_PASSWORD, AUTH_PUBLICKEY, AUTH_RHOSTS, AUTH_RHOSTS_RSA, AUTH_SDI, AUTH_TIS, authTypeDesc, CIPHER_3DES, CIPHER_ANY, CIPHER_BLOWFISH, CIPHER_DEFAULT, CIPHER_DES, CIPHER_IDEA, CIPHER_INVALID, CIPHER_NONE, CIPHER_NOTSUPPORTED, CIPHER_RC4, CIPHER_RESERVED, CIPHER_TSS, cipherClasses, cipherType, CMSG_ACM_ACCESS_DENIED, CMSG_ACM_NEW_PIN, CMSG_ACM_NEW_PIN_ACCEPTED, CMSG_ACM_NEW_PIN_REJECTED, CMSG_ACM_NEW_PIN_REQUIRED, CMSG_ACM_NEXT_CODE, CMSG_ACM_NEXT_CODE_REQUIRED, CMSG_ACM_OK, CMSG_AGENT_REQUEST_FORWARDING, CMSG_AUTH_PASSWORD, CMSG_AUTH_RHOSTS, CMSG_AUTH_RHOSTS_RSA, CMSG_AUTH_RSA, CMSG_AUTH_RSA_RESPONSE, CMSG_AUTH_SDI, CMSG_AUTH_TIS, CMSG_AUTH_TIS_RESPONSE, CMSG_EOF, CMSG_EXEC_CMD, CMSG_EXEC_SHELL, CMSG_EXIT_CONFIRMATION, CMSG_MAX_PACKET_SIZE, CMSG_PORT_FORWARD_REQUEST, CMSG_REQUEST_COMPRESSION, CMSG_REQUEST_PTY, CMSG_SESSION_KEY, CMSG_STDIN_DATA, CMSG_USER, CMSG_WINDOW_SIZE, CMSG_X11_REQUEST_FORWARDING, CONNECT_CHAN_NUM, DEBUG, DEBUGMORE, DEBUGPKG, DEFAULTPORT, HOST_KEY_LENGTH, IDX_CIPHER_CLASS, IDX_CIPHER_NAME, isAnSSHClient, KNOWN_HOSTS_FILE, LISTEN_CHAN_NUM, MAIN_CHAN_NUM, MSG_ANY, MSG_CHANNEL_CLOSE, MSG_CHANNEL_CLOSE_CONFIRMATION, MSG_CHANNEL_DATA, MSG_CHANNEL_INPUT_EOF, MSG_CHANNEL_OPEN_CONFIRMATION, MSG_CHANNEL_OPEN_FAILURE, MSG_CHANNEL_OUTPUT_CLOSED, MSG_DEBUG, MSG_DISCONNECT, MSG_IGNORE, MSG_NONE, MSG_PORT_OPEN, PASS_KERBEROS_TGT, protocolFlags, PROTOFLAG_HOST_IN_FWD_OPEN, PROTOFLAG_SCREEN_NUMBER, PROXY_HTTP, PROXY_NONE, PROXY_NOTSUPPORTED, PROXY_SOCKS4, PROXY_SOCKS5_DNS, PROXY_SOCKS5_IP, randomSeed, rcvCipher, rcvComp, secureRandom, SERVER_KEY_LENGTH, SESSION_KEY_LENGTH, sessionId, sessionKey, SMSG_AGENT_OPEN, SMSG_AUTH_RSA_CHALLENGE, SMSG_AUTH_TIS_CHALLENGE, SMSG_EXITSTATUS, SMSG_FAILURE, SMSG_PUBLIC_KEY, SMSG_STDERR_DATA, SMSG_STDOUT_DATA, SMSG_SUCCESS, SMSG_X11_OPEN, sndCipher, sndComp, SRV_HOSTKEY_CHANGED, SRV_HOSTKEY_KNOWN, SRV_HOSTKEY_NEW, srvCookie, srvHostKey, srvServerKey, SSH_VER_MAJOR, SSH_VER_MINOR, supportedAuthTypes, supportedCiphers, TTY_OP_END, TTY_OP_ISPEED, TTY_OP_OSPEED, UNKNOWN_CHAN_NUM, VER_MINDTERM, VER_MINDTUNL
 
Constructor Summary
SSHClient(SSHAuthenticator authenticator, SSHClientUser user)
           
 
Method Summary
 void addLocalPortForward(int localPort, java.lang.String remoteHost, int remotePort, java.lang.String plugin)
           
 void addLocalPortForward(java.lang.String localHost, int localPort, java.lang.String remoteHost, int remotePort, java.lang.String plugin)
           
 int addRef()
           
 void addRemotePortForward(java.lang.String remoteHost, int remotePort, java.lang.String localHost, int localPort, java.lang.String plugin)
           
protected  void boot(boolean haveCnxWatch, NetworkConnection tpSocket)
           
 void bootSSH(boolean haveCnxWatch)
           
 void bootSSH(boolean haveCnxWatch, boolean releaseConnection)
           
 void clearAllForwards()
           
 void completed(java.nio.ByteBuffer buf)
          Called once the network read operation has been completed
 void connected(boolean timeout)
          Called once the connection has been established (assuming interest for this has been registered by calling the NotifyWhenConnected method of Switchboard).
 void connectionFailed(java.lang.Exception e)
          Called if the connection failed (assuming interest for this has been registered by calling the NotifyWhenConnected method of Switchboard).
 void delLocalPortForward(java.lang.String localHost, int port)
           
 int delRef()
           
 void delRemotePortForward(java.lang.String plugin)
           
 void delRemotePortForward(java.lang.String remoteHost, int port)
           
protected  void disconnect(boolean graceful)
           
protected  void disconnect(boolean graceful, java.lang.Exception e)
           
 void doSingleCommand(java.lang.String commandLine, boolean background, long msTimeout)
           
 void forcedDisconnect()
           
 long getConnectTimeout()
           
 SSHConsole getConsole()
           
 java.lang.Integer getExitStatus()
          Returns the exit status if it was set or null otherwise.
 long getHelloTimeout()
           
 java.net.InetAddress getLocalAddr()
           
 java.net.InetAddress getServerAddr()
           
 java.net.InetAddress getServerRealAddr()
           
 java.lang.String getServerVersion()
           
 boolean isConnected()
           
 boolean isOpened()
           
 void readFailed(java.lang.Exception e)
          Called if the read failed
 void requestLocalPortForward(java.lang.String localHost, int localPort, java.lang.String remoteHost, int remotePort, java.lang.String plugin)
           
 void setConsole(SSHConsole console)
           
protected  void setExitStatus(int status)
          Called from SSHChannelController to set the exit status.
 void setLocalAddr(java.lang.String addr)
           
 void setServerRealAddr(java.net.InetAddress realAddr)
           
protected  void shutdown(java.lang.Exception e)
          Shut down the connection
 void startExitMonitor()
           
 void startExitMonitor(long msTimeout)
           
 void waitForExit()
           
 void waitForExit(long msTimeout)
           
 void writeFailed()
          Called if the write failed
 
Methods inherited from class com.mindbright.ssh.SSH
getAltAuthName, getAuthName, getAuthType, getAuthTypes, getCipherName, getCipherType, getProxyType, getProxyTypes, getSupportedCipher, getVersionId, haveSecureRandom, initCipher, initClientCipher, initSeedGenerator, initServerCipher, isAuthTypeSupported, isProtocolFlagSet, log, logDebug, logExtra, logIgnore, msgTypeString, randomSeed, secureRandom
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

heartbeat

protected com.mindbright.ssh.SSHClient.KeepAliveThread heartbeat

rand

protected SecureRandomAndPad rand

serverAddr

protected java.net.InetAddress serverAddr

serverRealAddr

protected java.net.InetAddress serverRealAddr

localAddr

protected java.net.InetAddress localAddr

srvVersionStr

protected java.lang.String srvVersionStr

srvVersionMajor

protected int srvVersionMajor

srvVersionMinor

protected int srvVersionMinor

localForwards

protected java.util.Vector<SSHClient.LocalForward> localForwards

remoteForwards

protected java.util.Vector<SSHClient.RemoteForward> remoteForwards

commandLine

protected java.lang.String commandLine

controllerMonitor

protected java.lang.Object controllerMonitor

controller

protected SSHChannelController controller

console

protected SSHConsole console

authenticator

protected SSHAuthenticator authenticator

user

protected SSHClientUser user

interactor

protected SSHInteractor interactor

sshSocket

protected NetworkConnection sshSocket

sshIn

protected NonBlockingInput sshIn

sshOut

protected NonBlockingOutput sshOut

gracefulExit

protected boolean gracefulExit

isConnected

protected boolean isConnected

isOpened

protected boolean isOpened

refCount

protected int refCount

havePORTFtp

protected boolean havePORTFtp

firstFTPPort

protected int firstFTPPort

activateTunnels

protected boolean activateTunnels

isReading

public boolean isReading
SSHClient will set this variable to true when it is reading from the input connection. So subclasses which implement NIOCallback should test this in completed() and call the completed() in SSHClient when it is set to true.


exitStatus

protected java.lang.Integer exitStatus
Exit code.

Constructor Detail

SSHClient

public SSHClient(SSHAuthenticator authenticator,
                 SSHClientUser user)
Method Detail

setConsole

public void setConsole(SSHConsole console)

getConsole

public SSHConsole getConsole()

getServerAddr

public java.net.InetAddress getServerAddr()

getServerRealAddr

public java.net.InetAddress getServerRealAddr()

setServerRealAddr

public void setServerRealAddr(java.net.InetAddress realAddr)

getLocalAddr

public java.net.InetAddress getLocalAddr()

setLocalAddr

public void setLocalAddr(java.lang.String addr)
                  throws java.net.UnknownHostException
Throws:
java.net.UnknownHostException

getServerVersion

public java.lang.String getServerVersion()

addLocalPortForward

public void addLocalPortForward(int localPort,
                                java.lang.String remoteHost,
                                int remotePort,
                                java.lang.String plugin)
                         throws java.io.IOException
Throws:
java.io.IOException

addLocalPortForward

public void addLocalPortForward(java.lang.String localHost,
                                int localPort,
                                java.lang.String remoteHost,
                                int remotePort,
                                java.lang.String plugin)
                         throws java.io.IOException
Throws:
java.io.IOException

delLocalPortForward

public void delLocalPortForward(java.lang.String localHost,
                                int port)

addRemotePortForward

public void addRemotePortForward(java.lang.String remoteHost,
                                 int remotePort,
                                 java.lang.String localHost,
                                 int localPort,
                                 java.lang.String plugin)

delRemotePortForward

public void delRemotePortForward(java.lang.String remoteHost,
                                 int port)

delRemotePortForward

public void delRemotePortForward(java.lang.String plugin)

clearAllForwards

public void clearAllForwards()

startExitMonitor

public void startExitMonitor()

startExitMonitor

public void startExitMonitor(long msTimeout)

addRef

public int addRef()

forcedDisconnect

public void forcedDisconnect()

delRef

public int delRef()

waitForExit

public void waitForExit()

waitForExit

public void waitForExit(long msTimeout)

doSingleCommand

public void doSingleCommand(java.lang.String commandLine,
                            boolean background,
                            long msTimeout)
                     throws java.io.IOException
Throws:
java.io.IOException

getConnectTimeout

public long getConnectTimeout()

getHelloTimeout

public long getHelloTimeout()

bootSSH

public void bootSSH(boolean haveCnxWatch)
             throws java.io.IOException
Throws:
java.io.IOException

bootSSH

public void bootSSH(boolean haveCnxWatch,
                    boolean releaseConnection)
             throws java.io.IOException
Throws:
java.io.IOException

boot

protected void boot(boolean haveCnxWatch,
                    NetworkConnection tpSocket)

setExitStatus

protected void setExitStatus(int status)
Called from SSHChannelController to set the exit status.


getExitStatus

public java.lang.Integer getExitStatus()
Returns the exit status if it was set or null otherwise.


disconnect

protected void disconnect(boolean graceful)

disconnect

protected void disconnect(boolean graceful,
                          java.lang.Exception e)

requestLocalPortForward

public void requestLocalPortForward(java.lang.String localHost,
                                    int localPort,
                                    java.lang.String remoteHost,
                                    int remotePort,
                                    java.lang.String plugin)
                             throws java.io.IOException
Throws:
java.io.IOException

isOpened

public boolean isOpened()

isConnected

public boolean isConnected()

shutdown

protected void shutdown(java.lang.Exception e)
Shut down the connection


completed

public void completed(java.nio.ByteBuffer buf)
Description copied from interface: NIOCallback
Called once the network read operation has been completed

Specified by:
completed in interface NIOCallback
Parameters:
buf - the buffer provided to the read call

readFailed

public void readFailed(java.lang.Exception e)
Description copied from interface: NIOCallback
Called if the read failed

Specified by:
readFailed in interface NIOCallback

writeFailed

public void writeFailed()
Description copied from interface: NIOCallback
Called if the write failed

Specified by:
writeFailed in interface NIOCallback

connected

public void connected(boolean timeout)
Description copied from interface: NIOCallback
Called once the connection has been established (assuming interest for this has been registered by calling the NotifyWhenConnected method of Switchboard).

Specified by:
connected in interface NIOCallback
Parameters:
timeout - true if the connection attempt timed out

connectionFailed

public void connectionFailed(java.lang.Exception e)
Description copied from interface: NIOCallback
Called if the connection failed (assuming interest for this has been registered by calling the NotifyWhenConnected method of Switchboard).

Specified by:
connectionFailed in interface NIOCallback
Parameters:
e - the exception the connection failed with.