SSHEXEC Task

## SSHEXEC

Since Apache Ant 1.6

### Description

Runs a command on a remote machine running SSH daemon.

Note: This task depends on external libraries not included in the Ant distribution. See Library Dependencies for more information. This task has been tested with JSCh 0.1.29 and above and won't work with versions of JSCh earlier than 0.1.28.

See also the scp task

### Parameters

Attribute | Description | Required
--- | --- | ---
host | The hostname or IP address of the remote host to which you wish to connect. | Yes
username | The username on the remote host to which you are connecting. | Yes
command | The command to run on the remote host. | Exactly one of the two
commandResource | The resource (file) that contains the commands to run on the remote host. Since Ant 1.7.1
port | The port to connect to on the remote host. | No; defaults to 22
trust | This trusts all unknown hosts if set to yes or true. Note: If you set this to false (the default), the host you connect to must be listed in your knownhosts file, this also implies that the file exists. | No; defaults to no
knownhosts | This sets the known hosts file to use to validate the identity of the remote host. This must be a SSH2 format file. SSH1 format is not supported. | No; defaults to ${user.home}/.ssh/known_hosts
failonerror | Whether to halt the build if the command does not complete successfully. | No; defaults to true
password | The password. | Yes, unless you are using key based authentication or the password has been given in the file or todir attribute.
keyfile | Location of the file holding the private key. | Yes, if you are using key based authentication
passphrase | Passphrase for your private key. | No; defaults to an empty string
sshConfig | Location of the file holding the OpenSSH style configuration (e.g. `${user.home}/.ssh/config`). The username and the key file are read from the configuration file, unless they are already specified in the task parameters. since Ant 1.10.8 | No
suppresssystemout | Whether to suppress system out. since Ant 1.9.0 | No; defaults to false
suppresssystemerr | Whether to suppress `System.err`. since Ant 1.9.4 | No; defaults to false
output | Name of a file to which to write the output. | No
errorOutput | The file to which the standard error of the command should be redirected. since Ant 1.9.4 | No
append | Whether output file should be appended to or overwritten. | No; defaults to false, meaning overwrite any existing file
errAppend | Whether errorOutput file should be appended to or overwritten. since Ant 1.9.4 | No; defaults to false, meaning overwrite any existing file
outputproperty | The name of a property in which the output of the command should be stored. If you use the commandResource attribute, each command's output will be prefixed by the command itself. | No
errorproperty | The name of a property in which the standard error of the command should be stored. since Ant 1.9.4 | No
resultproperty | the name of a property in which the return code of the command should be stored. Only of interest if failonerror=false. since Ant 1.9.4 | No
timeout | Stop the command if it doesn't finish within the specified time (given in milliseconds unlike `telnet`, which expects a timeout in seconds). | No; defaults to 0 which means never
input | A file from which the executed command's standard input is taken. This attribute is mutually exclusive with the inputstring and inputproperty attributes. When executing more than one command via commandResource, input will be read for each command. since Ant 1.8.0 | No
verbose | Determines whether `sshexec` outputs verbosely to the user. Similar output is generated as the `ssh` command line tool with the `-v` option. since Ant 1.8.0 | No; defaults to false
inputproperty | Name of a property content of which serves as the input stream for the executed command. This attribute is mutually exclusive with the input and inputstring attributes. When executing more than one command via commandResource, input will be read for each command. since Ant 1.8.0 | No
inputstring | A string which serves as the input stream for the executed command. This attribute is mutually exclusive with the input and inputproperty attributes. When executing more than one command via commandResource, input will be read for each command. since Ant 1.8.3 | No
usepty | Whether to allocate a pseudo-tty (like `ssh -t`). since Ant 1.8.3 | No; defaults to false
useSystemIn | Whether to pass the current standard input to the remote process. since Ant 1.9.4 | No; defaults to false
serverAliveIntervalSeconds | Sets a timeout interval in seconds after which if no data has been received from the server, the task will send a message through the encrypted channel to request a response from the server. since Ant 1.9.7 | No, the default is 0, indicating that these messages will not be sent to the server
serverAliveCountMax | The number of server alive messages which may be sent without receiving any messages back from the server. Only used if serverAliveIntervalSeconds is not 0. since Ant 1.9.7 | No; defaults to 3

### Parameters specified as nested elements

#### additionalConfig

since Ant 1.10.10

Adds configuration settings for the JSch Session created that are not directly supported by specific Ant attributes.

Attribute | Description | Required
--- | --- | ---
key | The key of the configuration setting. | Yes
value | The value of the configuration setting. | Yes

### Examples

Run a command on a remote machine using password authentication

    <sshexec host="somehost"
             username="dude"
             password="yo"
             command="touch somefile"/>

Run a command on a remote machine using key authentication

    <sshexec host="somehost"
             username="dude"
             keyfile="${user.home}/.ssh/id_dsa"
             passphrase="yo its a secret"
             command="touch somefile"/>

Run a command on a remote machine using key authentication with no passphrase

    <sshexec host="somehost"
             username="dude"
             keyfile="${user.home}/.ssh/id_dsa"
             command="touch somefile"/>

Run a set of commands from a command resource (file) on a remote machine using key authentication with no passphrase

    <sshexec host="somehost"
             username="dude"
             keyfile="${user.home}/.ssh/id_dsa"
             commandResource="to_run"/>

Security Note: Hardcoding passwords and/or usernames in sshexec task can be a serious security hole. Consider using variable substitution and include the password on the command line. For example:

    <sshexec host="somehost"
             username="${username}"
             password="${password}"
             command="touch somefile"/>

Invoking Ant with the following command line:

    ant -Dusername=me -Dpassword=mypassword target1 target2

is slightly better, but the username/password is exposed to all users on an Unix system (via the `ps` command). The best approach is to use the `<input>` task and/or retrieve the password from a (secured) `.properties` file.