verinec.util
Class J2sshProcess

java.lang.Object
  extended by java.lang.Process
      extended by verinec.util.J2sshProcess

public class J2sshProcess
extends Process

Process implementation for J2ssh.

J2ssh developpment has been abandoned, because the company behind has started a commercial rewrite of the library.
J2ssh seems to have some weaknesses. On sensitive systems, an other implementation should be used - if you use only linux, you could also build an scp distributor using the ProgramExec method to call scp.

Original J2ssh copyright notice:
J2ssh is Copyright (C) 2002-2003 Lee David Painter and Contributors.
See www.sshtools.com for more information about j2ssh.

However, at sshtools.com, they sell a new implementation. The library we use within VeriNeC is still online at sourceforge.net/projects/sshtools

Version:
$Revision: 1044 $
Author:
david.buchmann at unifr.ch
See Also:
SshProgramExec

Nested Class Summary
 class J2sshProcess.ProcessThread
          A thread to execute a command on a channel.
static class J2sshProcess.SingleKnownHostsKeyVerification
          Used if the fingerprint of the target host is specified.
 
Field Summary
private  String cmdline
           
private  File cwd
           
private  InputStream error
          Input stream for process error output.
private  J2sshProcess.ProcessThread executer
           
private  PipedInputStream input
          Input stream of process output.
private  PipedOutputStream output
          Output stream for process input.
private  int retval
           
private  com.sshtools.j2ssh.SshClient ssh
           
 
Constructor Summary
J2sshProcess(String cmdline, com.sshtools.j2ssh.SshClient ssh, File cwd)
          Start a program execution process.
 
Method Summary
 void destroy()
          Kills the subprocess.
 int exitValue()
          Returns the exit value for the subprocess.
 InputStream getErrorStream()
          Gets the error stream of the subprocess.
 InputStream getInputStream()
          Gets the input stream of the subprocess.
 OutputStream getOutputStream()
          Gets the output stream of the subprocess.
static com.sshtools.j2ssh.SshClient getSshConnection(String host, int port, String username, String password, String prefPublicKey, File keyfile, String passphrase, String hostVerification)
          Perform all the messy work of setting up the ssh connection.
static com.sshtools.j2ssh.SshClient getSshConnection(String username, String password, File keyfile, String passphrase, com.sshtools.j2ssh.configuration.SshConnectionProperties props, com.sshtools.j2ssh.transport.HostKeyVerification hv)
          Perform all the messy work of setting up the ssh connection.
 int waitFor()
          causes the current thread to wait, if necessary, until the process represented by this Process object has terminated.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

executer

private J2sshProcess.ProcessThread executer

ssh

private com.sshtools.j2ssh.SshClient ssh

cmdline

private String cmdline

retval

private int retval

cwd

private File cwd

error

private InputStream error
Input stream for process error output.


input

private PipedInputStream input
Input stream of process output.


output

private PipedOutputStream output
Output stream for process input.

Constructor Detail

J2sshProcess

public J2sshProcess(String cmdline,
                    com.sshtools.j2ssh.SshClient ssh,
                    File cwd)
             throws VerinecException
Start a program execution process. Starts a separate thread to execute the program. Caller must take care to close connection when all command executions have been terminated.

Parameters:
cmdline - The command line to execute
ssh - The client to use. Connection must be established.
cwd - The current working directory (on the remote system). If null, no cwd is used.
Throws:
VerinecException - If creating ssh process fails.
Method Detail

destroy

public void destroy()
Kills the subprocess. The subprocess represented by this Process object is forcibly terminated.

Specified by:
destroy in class Process

exitValue

public int exitValue()
Returns the exit value for the subprocess.

Specified by:
exitValue in class Process
Returns:
The exit value of the subprocess represented by this Process object. By convention, the value 0 indicates normal termination.
Throws:
IllegalThreadStateException - if the subprocess represented by this Process object has not yet terminated.

getErrorStream

public InputStream getErrorStream()
Gets the error stream of the subprocess. The stream obtains data piped from the error output stream of the process represented by this Process object.

Specified by:
getErrorStream in class Process
Returns:
the input stream connected to the error stream of the subprocess.

getInputStream

public InputStream getInputStream()
Gets the input stream of the subprocess. The stream obtains data piped from the standard output stream of the process represented by this Process object.

Specified by:
getInputStream in class Process
Returns:
the input stream connected to the normal output of the subprocess.

getOutputStream

public OutputStream getOutputStream()
Gets the output stream of the subprocess. Output to the stream is piped into the standard input stream of the process represented by this Process object.

Specified by:
getOutputStream in class Process
Returns:
the output stream connected to the input of the subprocess.

waitFor

public int waitFor()
            throws InterruptedException
causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.

Specified by:
waitFor in class Process
Returns:
the exit value of the process. By convention, 0 indicates normal termination.
Throws:
InterruptedException - if the current thread is interrupted by another thread while it is waiting, then the wait is ended and an InterruptedException is thrown.

getSshConnection

public static com.sshtools.j2ssh.SshClient getSshConnection(String host,
                                                            int port,
                                                            String username,
                                                            String password,
                                                            String prefPublicKey,
                                                            File keyfile,
                                                            String passphrase,
                                                            String hostVerification)
                                                     throws VerinecException
Perform all the messy work of setting up the ssh connection.

Parameters:
host - Hostname to connect to
port - A port number to use, pass -1 to leave default
username - The username to log into the remote system.
password - The password to use for password authentication. If null, no password auth is attempted.
prefPublicKey - The preferred public key algorithm. 'ssh-rsa' is a good choice.
keyfile - The path to file containing the private keys. If null, no public key auth is attempted.
passphrase - The passphrase to use with the key file. Pass null if no pass phrase needed.
hostVerification - Requirement for host verification. Possible values are ignore, interactive or the expected host fingerprint.
Returns:
A ssh connection to the target system
Throws:
VerinecException - If the connection can not be established
IllegalArgumentException - If one of the arguments can not be used.

getSshConnection

public static com.sshtools.j2ssh.SshClient getSshConnection(String username,
                                                            String password,
                                                            File keyfile,
                                                            String passphrase,
                                                            com.sshtools.j2ssh.configuration.SshConnectionProperties props,
                                                            com.sshtools.j2ssh.transport.HostKeyVerification hv)
                                                     throws VerinecException
Perform all the messy work of setting up the ssh connection. Use getSshConnection(String,int,String,String,String,File,String,String) if you do not want to set up the connection properties yourselves.

Parameters:
username - The username to log into the remote system.
password - The password to use for password authentication. If null, no password auth is attempted.
keyfile - The path to file containing the private keys. If null, no public key auth is attempted.
passphrase - The passphrase to use with the key file. Pass null if no pass phrase needed.
props - The connection properties, setting host, port, preferred algorithm ecc.
hv - The desired HostKeyVerification instance.
Returns:
A ssh connection to the target system
Throws:
VerinecException - If the connection can not be established
IllegalArgumentException - If one of the arguments can not be used.

Copyright © 2005 Verinec, DIUF