org.jclouds.tools.ant.util
Class SSHExecute

java.lang.Object
  extended by org.jclouds.tools.ant.util.SSHExecute

public class SSHExecute
extends Object

Executes a command on a remote machine via ssh.

adapted from SSHBase and SSHExec ant tasks, and Execute from the ant 1.7.1 release.

Author:
Adrian Cole

Constructor Summary
SSHExecute()
          Creates a new execute object using PumpStreamHandler for stream handling.
SSHExecute(org.apache.tools.ant.taskdefs.ExecuteStreamHandler streamHandler)
          Creates a new ssh object.
 
Method Summary
static void closeStreams(com.jcraft.jsch.ChannelExec process)
          Close the streams belonging to the given Process.
 int execute(String command)
          Execute the command on the remote host.
protected  com.jcraft.jsch.Session openSession()
          Open an ssh seession.
 void setHost(String host)
          Remote host, either DNS name or IP.
 void setKeyfile(String keyfile)
          Sets the keyfile for the user.
 void setKnownhosts(String knownHosts)
          Sets the path to the file that has the identities of all known hosts.
 void setPassphrase(String passphrase)
          Sets the passphrase for the users key.
 void setPassword(String password)
          Sets the password for the user.
 void setPort(int port)
          Changes the port used to connect to the remote host.
 void setProject(org.apache.tools.ant.Project project)
          Used for logging
 void setStreamHandler(org.apache.tools.ant.taskdefs.ExecuteStreamHandler streamHandler)
          Set the stream handler to use.
 void setTimeout(long timeout)
          The connection can be dropped after a specified number of milliseconds.
 void setTrust(boolean yesOrNo)
          Setting this to true trusts hosts whose identity is unknown.
 void setUsername(String username)
          Username known to remote host.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SSHExecute

public SSHExecute()
Creates a new execute object using PumpStreamHandler for stream handling.


SSHExecute

public SSHExecute(org.apache.tools.ant.taskdefs.ExecuteStreamHandler streamHandler)
Creates a new ssh object.

Parameters:
streamHandler - the stream handler used to handle the input and output streams of the subprocess.
Method Detail

setStreamHandler

public void setStreamHandler(org.apache.tools.ant.taskdefs.ExecuteStreamHandler streamHandler)
Set the stream handler to use.

Parameters:
streamHandler - ExecuteStreamHandler.

setTrust

public void setTrust(boolean yesOrNo)
Setting this to true trusts hosts whose identity is unknown.

Parameters:
yesOrNo - if true trust the identity of unknown hosts.

setProject

public void setProject(org.apache.tools.ant.Project project)
Used for logging


setUsername

public void setUsername(String username)
Username known to remote host.

Parameters:
username - The new username value

setPassword

public void setPassword(String password)
Sets the password for the user.

Parameters:
password - The new password value

setKeyfile

public void setKeyfile(String keyfile)
Sets the keyfile for the user.

Parameters:
keyfile - The new keyfile value

setPassphrase

public void setPassphrase(String passphrase)
Sets the passphrase for the users key.

Parameters:
passphrase - The new passphrase value

setHost

public void setHost(String host)
Remote host, either DNS name or IP.

Parameters:
host - The new host value

setPort

public void setPort(int port)
Changes the port used to connect to the remote host.

Parameters:
port - port number of remote host.

setTimeout

public void setTimeout(long timeout)
The connection can be dropped after a specified number of milliseconds. This is sometimes useful when a connection may be flaky. Default is 0, which means "wait forever".

Parameters:
timeout - The new timeout value in seconds

setKnownhosts

public void setKnownhosts(String knownHosts)
Sets the path to the file that has the identities of all known hosts. This is used by SSH protocol to validate the identity of the host. The default is ${user.home}/.ssh/known_hosts.

Parameters:
knownHosts - a path to the known hosts file.

execute

public int execute(String command)
            throws org.apache.tools.ant.BuildException,
                   com.jcraft.jsch.JSchException,
                   IOException,
                   TimeoutException
Execute the command on the remote host.

Parameters:
command - - what to execute on the remote host.
Returns:
return code of the process.
Throws:
org.apache.tools.ant.BuildException - bad parameter.
com.jcraft.jsch.JSchException - if there's an underlying problem exposed in SSH
IOException - if there's a problem attaching streams.
TimeoutException - if we exceeded our timeout

openSession

protected com.jcraft.jsch.Session openSession()
                                       throws com.jcraft.jsch.JSchException
Open an ssh seession.

Returns:
the opened session
Throws:
com.jcraft.jsch.JSchException - on error

closeStreams

public static void closeStreams(com.jcraft.jsch.ChannelExec process)
                         throws IOException
Close the streams belonging to the given Process.

Parameters:
process - the Process.
Throws:
IOException


Copyright © 2009-2012 jclouds. All Rights Reserved.