com.ace.moab.api.connect
Class SSHConnection

java.lang.Object
  extended by com.ace.moab.api.connect.SSHConnection
All Implemented Interfaces:
IMoabConnection

public class SSHConnection
extends java.lang.Object
implements IMoabConnection

Provides a connection to an instance of Moab through an SSH connection. This class also provides some methods to get more information about the Moab Connection specific to SSH connections.

Author:
Scott Brown
See Also:
IMoabConnection

Constructor Summary
SSHConnection(java.lang.String hostname, int port)
          This constructor sets up the host and port information necessary to create an SSH connection.
 
Method Summary
 boolean connectWithPassword(java.lang.String username, java.lang.String password)
          Initiates a connection to the host machine on the port number previously specified.
 boolean connectWithPublicKey(java.lang.String username, java.lang.String publicKeyFilePath, java.lang.String keyPassphrase)
          Initiates a connection to the host machine on the port number previously specified.
 void disconnect()
          Disconnects the ssh connection if it is active.
 MoabInputStream executeCommand(java.lang.String command, boolean isMoabCommand)
          Same as connect(command, isMoabCommand, null).
 MoabInputStream executeCommand(java.lang.String command, boolean isMoabCommand, org.apache.log4j.Level log4jLevel)
           Runs a command on the remote machine and returns back a reference to the intputStream which will output the results of that command.
 ConnectionType getConnectionType()
          Returns the connection type of SSH.
 java.lang.String getHostname()
          Gets the hostname specified at creation time.
 boolean isConnected()
          Returns true if this connection has been established and authenticated.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SSHConnection

public SSHConnection(java.lang.String hostname,
                     int port)
This constructor sets up the host and port information necessary to create an SSH connection. All SSH connections require a hostname and a port to connect to. This method however does not initiate any connections nor checks if the hostname or port is valid.

Parameters:
hostname - The name of the host machine that the connection will be made with.
port - The port number of the host machine that the connection will be made with.
Method Detail

connectWithPassword

public boolean connectWithPassword(java.lang.String username,
                                   java.lang.String password)
                            throws java.io.IOException,
                                   AuthenticationException
Initiates a connection to the host machine on the port number previously specified. This method will attempt to establish a connection with the host machine and then authenticate with the specified username and password.

If the authentication phase is complete, true will be returned. If the server does not accept the request (or if further authentication steps are needed), false is returned and one can retry either by using this or any other authentication method

Note: if this method fails, then please double-check that it is actually offered by the server

Often, password authentication is disabled, but users are not aware of it. Many servers only offer "publickey" and "keyboard-interactive". However, even though "keyboard-interactive" *feels* like password authentication (e.g., when using the putty or openssh clients) it is *not* the same mechanism.

Parameters:
username - The username used to connect to the host machine.
password - The password used to connect to the host machine.
Returns:
if the connection is now authenticated.
Throws:
java.io.IOException
AuthenticationException - If the authentication failed, an AuthenticationException is thrown.

connectWithPublicKey

public boolean connectWithPublicKey(java.lang.String username,
                                    java.lang.String publicKeyFilePath,
                                    java.lang.String keyPassphrase)
                             throws java.io.IOException,
                                    com.moab.api.connect.exception.InitialConnectException
Initiates a connection to the host machine on the port number previously specified. This method will attempt to establish a connection with the host machine and then authenticate with the standard "ssh-rsa" public-key method.

If the authentication phase is complete, true will be returned. If the server does not accept the request (or if further authentication steps are needed), false is returned and one can retry either by using this or any other authentication method.

Parameters:
username - The user used to connect to Moab.
publicKeyFilePath - The complete path of the public key. (i.e. /home/tmp/.ssh/id_rsa)
keyPassphrase - The passphrase used to unlock the key file.
Returns:
true if connection was successful; false, if not successful
Throws:
java.io.IOException
com.moab.api.connect.exception.InitialConnectException

disconnect

public void disconnect()
Disconnects the ssh connection if it is active.


getHostname

public java.lang.String getHostname()
Gets the hostname specified at creation time.

Returns:
the hostname specified at creation time.

isConnected

public boolean isConnected()
Returns true if this connection has been established and authenticated. If true, users can attempt to execute commands on the host.

Returns:
if the connection has been established and authenticated.

getConnectionType

public ConnectionType getConnectionType()
Returns the connection type of SSH.

Specified by:
getConnectionType in interface IMoabConnection
Returns:
the ConnectionType.SSH enum value.
See Also:
ConnectionType

executeCommand

public MoabInputStream executeCommand(java.lang.String command,
                                      boolean isMoabCommand)
                               throws MoabCommunicationException
Same as connect(command, isMoabCommand, null).

Specified by:
executeCommand in interface IMoabConnection
Parameters:
command - the command to be run on the machine where Moab is running
isMoabCommand - whether the command is a Moab command.
Returns:
see comments for the executeCommand(String, boolean, Level) method.
Throws:
MoabCommunicationException - If the command is invalid or the connection has issues, a MoabCommunciationException is thrown.

executeCommand

public MoabInputStream executeCommand(java.lang.String command,
                                      boolean isMoabCommand,
                                      org.apache.log4j.Level log4jLevel)
                               throws MoabCommunicationException

Runs a command on the remote machine and returns back a reference to the intputStream which will output the results of that command. This can be a moab command, or any other command that can be run on the remote machine

Specified by:
executeCommand in interface IMoabConnection
Parameters:
command - the command to be run on the local machine
isMoabCommand - whether the command is a Moab command. (i.e. 'showq')
log4jLevel - (can be null) the log4j level at which this Moab command and it's output should be written to the log file. This allows users the ability to prevent the logs from becoming unnecessarily cluttered by frequently called Moab commands.
Returns:
the inputStream which will contain the results of the command.
Throws:
MoabCommunicationException - If the command is invalid or the connection has issues, a MoabCommunciationException is thrown.


Copyright © 2001-2009 Cluster Resources, Inc. All Rights Reserved.