|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.peace_tools.core.session.ServerSession
org.peace_tools.core.session.RemoteServerSession
public class RemoteServerSession
A remote server session based on the secure shell (SSH) protocol.
This class provides an implementation of the ServerSession API. Specifically, this class provides a session that can be used to interact with a remote host via the secure shell (SSH) protocol. The secure shell protocol is a defacto standard for interacting with remote servers via the Internet today. It provides all the necessary security features to safely interact with remote hosts and almost all super computing clusters mandate the use of SSH for interactions.
This class uses the Ganymede SSH implementation for establishing SSH connections. Ganymede SSH supports only ssh-2 protocol. Please refer to Genymede SSH website for further details: http://www.ganymed.ethz.ch/ssh2/. PEACE distributes Ganymede license file as per Ganymede licensing requirements.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class org.peace_tools.core.session.ServerSession |
---|
ServerSession.OSType |
Field Summary | |
---|---|
private ch.ethz.ssh2.Connection |
connection
The connection to the remote server via which the remote server can be accessed for performing various operations. |
private static java.lang.String |
KnownHostPath
The OS-specific path where the list of known hosts (that is the servers to which we have connected before) is stored. |
private static ch.ethz.ssh2.KnownHosts |
knownHosts
This is a convenience class that is provided by Ganymede SSH to store information about servers/hosts that we have already connected to. |
private static java.lang.Object |
knownHostsLock
This object is merely used to arbitrate access to the knownHosts object so that operations on knownHosts is MT-Safe. |
private ServerSession.OSType |
osType
The last known OS type for the remote server. |
private java.lang.String |
purpose
A simple textual information indicating the purpose for this session. |
Fields inherited from class org.peace_tools.core.session.ServerSession |
---|
parent, server |
Constructor Summary | |
---|---|
protected |
RemoteServerSession(java.awt.Component parent,
Server server)
The constructor merely passes parameters to the base class that initializes the instance variables. |
Method Summary | |
---|---|
private void |
addKnownHost(java.lang.String hostname,
int port,
java.lang.String serverHostKeyAlgorithm,
byte[] serverHostKey)
Method to add a new host entry to the list of known hosts. |
void |
connect()
Connect to the server in order to perform various operations. |
void |
copy(java.io.InputStream srcData,
java.lang.String destDirectory,
java.lang.String destFileName,
java.lang.String mode)
Copy given data from an input stream to a given file on the remote machine. |
void |
copy(java.io.OutputStream destData,
java.lang.String srcDirectory,
java.lang.String srcFileName,
javax.swing.JProgressBar progBar)
Copy file from a remote machine to a given output stream. |
void |
disconnect()
Method to disconnect from a remote server. |
int |
exec(java.lang.String command,
javax.swing.text.DefaultStyledDocument output)
This method can be used to run a long running command that may produce verbose output. |
int |
exec(java.lang.String command,
java.lang.String[] outputs)
This method can be used to run a brief command that produces succinct output. |
FileInfo |
fstat(java.lang.String path)
Obtain information about a given path on the remote machine. |
ServerSession.OSType |
getOSType()
Determine the type of OS that this session is connected to. |
private void |
getPassword()
Helper method to check and get password from the user. |
private void |
loadKnownHosts()
This is a helper method that is invoked just before a remote session establishes connection with a server. |
void |
mkdir(java.lang.String directory)
Creates a directory on the server. |
void |
rmdir(java.lang.String directory)
Deletes an empty directory on the server. |
void |
setPurpose(java.lang.String text)
A simple method to set a purpose message for this session. |
boolean |
verifyServerHostKey(java.lang.String hostname,
int port,
java.lang.String serverHostKeyAlgorithm,
byte[] serverHostKey)
Interactive verifier for use with Ganymede SSH callback. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private ch.ethz.ssh2.Connection connection
private ServerSession.OSType osType
private static ch.ethz.ssh2.KnownHosts knownHosts
private static java.lang.Object knownHostsLock
private java.lang.String purpose
private static final java.lang.String KnownHostPath
Constructor Detail |
---|
protected RemoteServerSession(java.awt.Component parent, Server server)
server
- The server data entry that provides the necessary
information to connect to the server.parent
- The parent component that should be used to
create GUI elements that may be needed for any interactive
operations.Method Detail |
---|
public void connect() throws java.io.IOException
Note: The process of establishing a connection can be a time consuming task. In some cases incorrect host names can cause long connection times (until the connection times out which can be in minutes). Consequently, it is best to call this method from a separate daemon thread.
connect
in class ServerSession
java.io.IOException
- This method throws IO exceptions in
the case of errors. If an error occurs, then a connection
was not established and the caller will have to try again
to establish a connection.private void getPassword() throws java.io.IOException
java.io.IOException
public void disconnect()
disconnect
in class ServerSession
public int exec(java.lang.String command, java.lang.String[] outputs) throws java.lang.Exception
Note: The connection to the remote server must have been established successfully via a call to connect method.
exec
in class ServerSession
command
- The command line to be executed. This
command must be compatible with the target machine's
OS. Otherwise this method will generate an exception.outputs
- The buffered results from the standard
output and standard error streams of the remote process.
java.lang.Exception
- If the execution produces an error then
this method throws an exception.public int exec(java.lang.String command, javax.swing.text.DefaultStyledDocument output) throws java.lang.Exception
Note: The connection to the remote server must have been established successfully via a call to connect method.
exec
in class ServerSession
command
- The command line to be executed. This
command must be compatible with the target machine's
OS. Otherwise this method will generate an exception.output
- The styled document to which the standard
output and standard error streams are to be written.
java.lang.Exception
- If the execution produces an error then
this method throws an exception.public boolean verifyServerHostKey(java.lang.String hostname, int port, java.lang.String serverHostKeyAlgorithm, byte[] serverHostKey) throws java.lang.Exception
verifyServerHostKey
in interface ch.ethz.ssh2.ServerHostKeyVerifier
hostname
- The host name to be added to the list.port
- The port associated with the remote connection.
Currently this is not used.serverHostKeyAlgorithm
- The string name for the certificate
encryption algorithm (such as: ssh-rsa2 etc.)serverHostKey
- The host key (actual digest/figerprint)
java.lang.Exception
private void addKnownHost(java.lang.String hostname, int port, java.lang.String serverHostKeyAlgorithm, byte[] serverHostKey)
Note: This method checks to ensure that the host being added is indeed unique. This method consumes any exceptions generated by Ganymede (but logs it in the programmer log).
hostname
- The host name to be added to the list.port
- The port associated with the remote connection.
Currently this is not used.serverHostKeyAlgorithm
- The string name for the certificate
encryption algorithm (such as: ssh-rsa2 etc.)serverHostKey
- The host key (actual digest/figerprint)
as reported by the remote host.private void loadKnownHosts()
Note: This method loads the known hosts file only once, the first time it is called in the GUI process. Subsequent calls to this method simply return. Therefore calling this method frequently is OK.
public ServerSession.OSType getOSType() throws java.lang.Exception
Note: The session must be connected prior to this call.
getOSType
in class ServerSession
java.lang.Exception
- If the detection of OS fails or a connection
does not exist then this method throws an exception.public void copy(java.io.InputStream srcData, java.lang.String destDirectory, java.lang.String destFileName, java.lang.String mode) throws java.io.IOException
Note: This method uses SFTP to copy the data. The connection to the remote host must have already been established before invoking this method.
copy
in class ServerSession
srcData
- The source stream that provides the data to
be copied.destDirectory
- The destination directory to which the
data is to be copied. This method assumes that the remote
directory has already been created.destFileName
- The name of the destination file to which
the data is to be copied.mode
- The POSIX compliant mode string (such as: "0600"
or "0777" to be used as the mode for the target file.
java.io.IOException
- This method throws exceptions on errors.public void copy(java.io.OutputStream destData, java.lang.String srcDirectory, java.lang.String srcFileName, javax.swing.JProgressBar progBar) throws java.io.IOException
Note: This method uses SFTP to copy the data. The connection to the remote host must have already been established before invoking this method.
copy
in class ServerSession
destData
- The destination stream to which the data is
to be written.srcDirectory
- The source directory from where the
file is to be copied.srcFileName
- The name of the source file from where
the data is to be copied.progBar
- The progress bar to be used indicate the
file copy progress.
java.io.IOException
- This method throws exceptions on errors.public FileInfo fstat(java.lang.String path) throws java.io.IOException
Note: This method uses SFTP to copy the data. The connection to the remote host must have already been established before invoking this method.
fstat
in class ServerSession
path
- The path (absolute or relative to home directory)
of the file whose meta data is to be retrieved.
java.io.IOException
- This method throws exceptions on errors.public void mkdir(java.lang.String directory) throws java.lang.Exception
Note: The connection to the server must have already been successfully established via a call to the connect method before invoking this method.
mkdir
in class ServerSession
directory
- The fully path to the directory to be created.
java.io.IOException
- This method throws an exception if the
directory could not be created.
java.lang.Exception
public void rmdir(java.lang.String directory) throws java.lang.Exception
Note: The connection to the server must have already been successfully established via a call to the connect method before invoking this method.
rmdir
in class ServerSession
directory
- The fully path to the directory to be deleted.
java.io.IOException
- This method throws an exception if the
directory could not be deleted.
java.lang.Exception
public void setPurpose(java.lang.String text)
setPurpose
in class ServerSession
text
- This string is used to create a label (possibly
with an icon on it). So it can be plain text or HTML. If the
message is long, then ensure it is properly broken into
multiple lines so that dialog boxes don't get too large.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |