org.gridbus.broker.util
Class SSHSession

java.lang.Object
  extended by org.gridbus.broker.util.SSHSession

public class SSHSession
extends java.lang.Object

Author:
Marcos Represents an SSH Session with a host node.

Constructor Summary
SSHSession(java.lang.String host, java.lang.String user, java.lang.String pass)
          Constructor receives hostname, user and password
SSHSession(java.lang.String hostname, java.lang.String username, java.lang.String password, int port)
           
 
Method Summary
 void close()
          Closes session
 void connect()
          initializes the SSH session
 boolean detectDir(java.lang.String remoteDir)
          Detect directory on the host host by using SFTP
 boolean detectFile(java.lang.String remoteFile)
          Detect file on the host host by using SFTP
 boolean detectFile(java.lang.String filename, long timeoutMillis)
          Detects the existence of the remote file.
 java.lang.String executeAndGetResult(java.lang.String command)
           
 java.lang.String executeAndGetResult(java.lang.String command, long timeout)
          Execute a command on the host node
 void executeCmd(java.lang.String command)
          Execute a command on the host node
 int getPort()
          Gets SSH port
 java.lang.String getRemoteHost()
          Returns the server hostname
 java.lang.String getUsername()
          Returns the username.
 com.sshtools.j2ssh.session.SessionChannelClient openChannel(java.lang.String type)
          open a session channel for command execution
 java.lang.String[] resolvePaths(java.lang.String[] relativePaths, boolean createDirs)
          Resolves a set of given paths on the remote machine.
 boolean scpFrom(java.lang.String remoteFile, java.lang.String localFile)
          Copy a file from host node using SSH/SCP
 boolean scpFrom(java.lang.String remoteFile, java.lang.String localFile, int tries)
          Copies a file from a host node by using SCP.
 boolean scpTo(java.io.InputStream stream, long size, java.lang.String localFile, java.lang.String remoteFile)
          Send a file to host node by SCP
 boolean scpTo(java.lang.String localFile, java.lang.String remoteFile)
          Send a file to host node by SCP
 void setTimeout(int milliseconds)
          Sets connection timeout
 boolean sFtpFrom(java.lang.String remoteFile, java.lang.String localFile)
          Copies a file from a host host by using SFTP.
 boolean sFtpFrom(java.lang.String remoteFile, java.lang.String localFile, int tries)
          Copies a file from a host host by using SFTP.
 boolean sFtpTo(java.lang.String localFile, java.lang.String remoteFile)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SSHSession

public SSHSession(java.lang.String host,
                  java.lang.String user,
                  java.lang.String pass)
Constructor receives hostname, user and password

Parameters:
host -
user -
pass -

SSHSession

public SSHSession(java.lang.String hostname,
                  java.lang.String username,
                  java.lang.String password,
                  int port)
Parameters:
hostname -
username -
password -
port -
Method Detail

getUsername

public java.lang.String getUsername()
Returns the username.

Returns:
string

getPort

public int getPort()
Gets SSH port

Returns:
port

getRemoteHost

public java.lang.String getRemoteHost()
Returns the server hostname

Returns:
hostname

setTimeout

public void setTimeout(int milliseconds)
Sets connection timeout

Parameters:
milliseconds -

connect

public void connect()
             throws java.lang.Exception
initializes the SSH session

Throws:
java.lang.Exception

openChannel

public com.sshtools.j2ssh.session.SessionChannelClient openChannel(java.lang.String type)
                                                            throws java.lang.Exception
open a session channel for command execution

Parameters:
type -
Returns:
SessionChannelClient
Throws:
java.lang.Exception

scpTo

public boolean scpTo(java.lang.String localFile,
                     java.lang.String remoteFile)
Send a file to host node by SCP

Parameters:
remoteFile -
localFile -
Returns:
true if the file was been transmitted

scpTo

public boolean scpTo(java.io.InputStream stream,
                     long size,
                     java.lang.String localFile,
                     java.lang.String remoteFile)
Send a file to host node by SCP

Parameters:
stream -
size -
localFile -
remoteFile -
Returns:

scpFrom

public boolean scpFrom(java.lang.String remoteFile,
                       java.lang.String localFile,
                       int tries)
Copies a file from a host node by using SCP. This method tries to check if the file exists. You can specify the number of seconds it will try to check for the host file on the host node

Parameters:
remoteFile -
localFile -
tries -
Returns:

scpFrom

public boolean scpFrom(java.lang.String remoteFile,
                       java.lang.String localFile)
Copy a file from host node using SSH/SCP

Parameters:
localFile -
remoteFile -
Returns:
true if the file was correctly copied back

sFtpTo

public boolean sFtpTo(java.lang.String localFile,
                      java.lang.String remoteFile)
Parameters:
localFile -
remoteFile -
Returns:

sFtpFrom

public boolean sFtpFrom(java.lang.String remoteFile,
                        java.lang.String localFile,
                        int tries)
                 throws java.lang.Exception
Copies a file from a host host by using SFTP.

Parameters:
remoteFile - Remote file name
localFile - Local file name
tries - Number of tries to check if the host file exist on the host host. Each second an attempt to check the file on the host node is done.
Returns:
true if the file was copied.
Throws:
java.lang.Exception

sFtpFrom

public boolean sFtpFrom(java.lang.String remoteFile,
                        java.lang.String localFile)
                 throws java.lang.Exception
Copies a file from a host host by using SFTP.

Parameters:
remoteFile - Remote file name
localFile - Local file name
Returns:
true if the file was copied.
Throws:
java.lang.Exception

detectFile

public boolean detectFile(java.lang.String remoteFile)
                   throws java.lang.Exception
Detect file on the host host by using SFTP

Parameters:
remoteFile - Remote file name
Returns:
true if the file exists on the host node
Throws:
java.lang.Exception

resolvePaths

public java.lang.String[] resolvePaths(java.lang.String[] relativePaths,
                                       boolean createDirs)
                                throws java.lang.Exception
Resolves a set of given paths on the remote machine. Creates the directories if specified.

Parameters:
relativePaths -
createDirs -
Returns:
String[] of resolved paths
Throws:
java.lang.Exception

detectDir

public boolean detectDir(java.lang.String remoteDir)
                  throws java.lang.Exception
Detect directory on the host host by using SFTP

Parameters:
remoteDir - Remote directory name
Returns:
true if the directory exists on the host node
Throws:
java.lang.Exception

detectFile

public boolean detectFile(java.lang.String filename,
                          long timeoutMillis)
Detects the existence of the remote file. Returns true, if the file exists, false otherwise.

Parameters:
filename -
timeoutMillis -
Returns:

executeAndGetResult

public java.lang.String executeAndGetResult(java.lang.String command)
                                     throws java.lang.Exception
Parameters:
command -
Returns:
Throws:
java.lang.Exception

executeAndGetResult

public java.lang.String executeAndGetResult(java.lang.String command,
                                            long timeout)
                                     throws java.lang.Exception
Execute a command on the host node

Parameters:
command -
Returns:
Throws:
java.lang.Exception

executeCmd

public void executeCmd(java.lang.String command)
                throws java.lang.Exception
Execute a command on the host node

Parameters:
command -
Throws:
java.lang.Exception

close

public void close()
Closes session


Copyright © The University of Melbourne. 2006