verinec.util
Class SshProgramExec

java.lang.Object
  extended by verinec.util.AbstractProgramExec
      extended by verinec.util.SshProgramExec

public class SshProgramExec
extends AbstractProgramExec

Wrapper methods for execution of native programs on any system using ssh. Currently always uses J2ssh. todo: we should make this more flexible. J2ssh is not very stable, maybe there are better implementations. See J2sshProcess for details about the J2ssh limitations.

These methods provide for simpler program execution, allowing to redirect program output or input to files (the < / > parameters in a shell), or retrieving the output as a stream. They allow to set a timeout after which program execution is interrupted.

You can execute a program and wait for its result, or execute it asynchronous, that is in a separate thread without waiting for it to finish.

Version:
$Revision: 754 $
Author:
david.buchmann at unifr.ch

Nested Class Summary
 
Nested classes/interfaces inherited from class verinec.util.AbstractProgramExec
AbstractProgramExec.StreamGobbler, AbstractProgramExec.TimeoutWatcher
 
Field Summary
private  String host
          Hostname to connect to
private  String hostVerification
          Requirement for host verification.
private  File keyfile
          The path to file containing the private keys.
private  String passphrase
          The passphrase to use with the key file.
private  String password
          The password to use for password authentication.
private  int port
          A port number to use, pass -1 to leave default
private  String prefPublicKey
          The preferred public key algorithm.
private  String username
          The username to log into the remote system.
 
Fields inherited from class verinec.util.AbstractProgramExec
cwd, timedOut
 
Constructor Summary
SshProgramExec(String commandline, File cwd, InputStream stdin, OutputStream stdout, OutputStream stderr, String host, int port, String username, String password, String prefPublicKey, File keyfile, String passphrase, String hostVerification)
          Prepare a program execution.
 
Method Summary
static int execSync(String commandline, File cwd, InputStream stdin, OutputStream stdout, OutputStream stderr, int timeout, String host, int port, String username, String password, String prefPublicKey, File keyfile, String passphrase, String hostVerification)
          Execute a program and wait for it to terminate.
protected  Process startProcess(String commandline, File cwd)
          Instantiate a process using Runtime.
 
Methods inherited from class verinec.util.AbstractProgramExec
abortExecution, execAsync, execSync, exitValue, hasFinished
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

host

private String host
Hostname to connect to


port

private int port
A port number to use, pass -1 to leave default


username

private String username
The username to log into the remote system.


password

private String password
The password to use for password authentication. If null, no password auth is attempted.


prefPublicKey

private String prefPublicKey
The preferred public key algorithm. 'ssh-rsa' is a good choice.


keyfile

private File keyfile
The path to file containing the private keys. If null, no public key auth is attempted.


passphrase

private String passphrase
The passphrase to use with the key file. If null, keyfile is opened without pass phrase.


hostVerification

private String hostVerification
Requirement for host verification. Possible values are ignore, interactive or the expected host fingerprint.

Constructor Detail

SshProgramExec

public SshProgramExec(String commandline,
                      File cwd,
                      InputStream stdin,
                      OutputStream stdout,
                      OutputStream stderr,
                      String host,
                      int port,
                      String username,
                      String password,
                      String prefPublicKey,
                      File keyfile,
                      String passphrase,
                      String hostVerification)
Prepare a program execution. Afterwards, you can either run the program synchronous or asynchronous.

The input for the program can be given as an InputStream that will be piped to the program as stdin. If you have a file as input, you can use java.io.FileInputStream. stdin can be null, in which case no input is given to the program.

The output can be pushed into OutputStreams for later treatment. If both streams are null, output is discarded. If the stdout stream is null only, errors are written into stderr, if stderr is null, both out and err written into stdout. Again, you can use FileOutputStream to write into files. The streams will be closed when the program execution is finished.

It is recommended to wrap the output streams with a BufferedOutputStream to avoid delays.

Parameters:
commandline - Name of the executable with all parameters. (Do not forget full path if executable is not in the path for programs).
cwd - The current working directory directory for the new process (defaults to the system property user.dir if null).
stdin - A Stream that will be fed to the process as input (ignored if null).
stdout - A Stream to write the program output into (discarded if null).
stderr - A Stream to write the program error output into (redirected to stdout if null).
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.
Method Detail

execSync

public static int execSync(String commandline,
                           File cwd,
                           InputStream stdin,
                           OutputStream stdout,
                           OutputStream stderr,
                           int timeout,
                           String host,
                           int port,
                           String username,
                           String password,
                           String prefPublicKey,
                           File keyfile,
                           String passphrase,
                           String hostVerification)
                    throws InterruptedException,
                           VerinecException
Execute a program and wait for it to terminate.

The input for the program can be given as an InputStream that will be piped to the program as stdin. If you have a file as input, you can use java.io.FileInputStream. stdin can be null, in which case no input is given to the program.

The output can be pushed into OutputStreams for later treatment. If both streams are null, output is discarded. If the stdout stream is null only, errors are written into stderr, if stderr is null, both out and err written into stdout. Again, you can use FileOutputStream to write into files. The streams will be closed when the program execution is finished.

It is recommended to wrap the output streams with a BufferedOutputStream to avoid delays.

Parameters:
commandline - Name of the executable with all parameters. (Do not forget full path if executable is not in the path for programs).
cwd - The current working directory directory for the new process (defaults to the system property user.dir if null).
stdin - A Stream that will be fed to the process as input (ignored if null).
stdout - A Stream to write the program output into (discarded if null).
stderr - A Stream to write the program error output into (redirected to stdout if null).
timeout - The time in seconds after which the execution should be interrupted (0 for no timeout).
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.
Returns:
The exit code of the program.
Throws:
InterruptedException - If the timeout occurs or the process is otherwise interrupted.
VerinecException - If other errors occur.

startProcess

protected Process startProcess(String commandline,
                               File cwd)
                        throws VerinecException
Instantiate a process using Runtime.

Specified by:
startProcess in class AbstractProgramExec
Parameters:
commandline - The command line to execute.
cwd - Current working directory on the target system.
Returns:
A running process.
Throws:
VerinecException - If the specified command can not be executed.

Copyright © 2005 Verinec, DIUF