You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sshexec.html 8.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <!--
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <html>
  16. <head>
  17. <meta http-equiv="Content-Language" content="en-us">
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>SSHEXEC Task</title>
  20. </head>
  21. <body>
  22. <h2><a name="sshexec">SSHEXEC</a></h2>
  23. <h3>Description</h3>
  24. <p><em>since Ant 1.6</em></p>
  25. <p>Runs a command on a remote machine running SSH daemon.
  26. </p>
  27. <p><b>Note:</b> This task depends on external libraries not included
  28. in the Ant distribution. See <a
  29. href="../install.html#librarydependencies">Library Dependencies</a>
  30. for more information. This task has been tested with jsch-0.1.29 and above
  31. and won't work with versions of jsch earlier than
  32. 0.1.28.</p>
  33. <p>See also the <a href="scp.html">scp task</a></p>
  34. <h3>Parameters</h3>
  35. <table border="1" cellpadding="2" cellspacing="0">
  36. <tr>
  37. <td valign="top"><b>Attribute</b></td>
  38. <td valign="top"><b>Description</b></td>
  39. <td align="center" valign="top"><b>Required</b></td>
  40. </tr>
  41. <tr>
  42. <td valign="top">host</td>
  43. <td valign="top">The hostname or IP address of the remote host to which you wish to connect.</td>
  44. <td valign="top" align="center">Yes</td>
  45. </tr>
  46. <tr>
  47. <td valign="top">username</td>
  48. <td valign="top">The username on the remote host to which you are connecting.</td>
  49. <td valign="top" align="center">Yes</td>
  50. </tr>
  51. <tr>
  52. <td valign="top">command</td>
  53. <td valign="top">The command to run on the remote host.</td>
  54. <td valian="top" align="center">Either this or commandResource must be set</td>
  55. </tr>
  56. <tr>
  57. <td valign="top">commandResource</td>
  58. <td valign="top">The resource (file) that contains the commands to run on the remote host.
  59. Since Ant 1.7.1</td>
  60. <td valian="top" align="center">Either this or command must be set</td>
  61. </tr>
  62. <tr>
  63. <td valign="top">port</td>
  64. <td valign="top">The port to connect to on the remote host.</td>
  65. <td valian="top" align="center">No, defaults to 22.</td>
  66. </tr>
  67. <tr>
  68. <td valign="top">trust</td>
  69. <td valign="top">This trusts all unknown hosts if set to yes/true.<br>
  70. <strong>Note</strong> If you set this to false (the default), the
  71. host you connect to must be listed in your knownhosts file, this
  72. also implies that the file exists.</td>
  73. <td valian="top" align="center">No, defaults to No.</td>
  74. </tr>
  75. <tr>
  76. <td valign="top">knownhosts</td>
  77. <td valign="top">This sets the known hosts file to use to validate
  78. the identity of the remote host. This must be a SSH2 format file.
  79. SSH1 format is not supported.</td>
  80. <td valian="top" align="center">No, defaults to
  81. ${user.home}/.ssh/known_hosts.</td>
  82. </tr>
  83. <tr>
  84. <td valign="top">failonerror</td>
  85. <td valign="top">Whether to halt the build if the command does not complete successfully.
  86. </td>
  87. <td valign="top" align="center">No; defaults to true.</td>
  88. </tr>
  89. <tr>
  90. <td valign="top">password</td>
  91. <td valign="top">The password.</td>
  92. <td valign="top" align="center">Not if you are using key based
  93. authentication or the password has been given in the file or
  94. todir attribute.</td>
  95. </tr>
  96. <tr>
  97. <td valign="top">keyfile</td>
  98. <td valign="top">Location of the file holding the private key.</td>
  99. <td valign="top" align="center">Yes, if you are using key based
  100. authentication.</td>
  101. </tr>
  102. <tr>
  103. <td valign="top">passphrase</td>
  104. <td valign="top">Passphrase for your private key.</td>
  105. <td valign="top" align="center">No, defaults to an empty string.</td>
  106. </tr>
  107. <tr>
  108. <td valign="top">output</td>
  109. <td valign="top">Name of a file to which to write the output.</td>
  110. <td align="center" valign="top">No</td>
  111. </tr>
  112. <tr>
  113. <td valign="top">append</td>
  114. <td valign="top">Whether output file should be appended to or overwritten. Defaults to false, meaning overwrite any existing file.</td>
  115. <td align="center" valign="top">No</td>
  116. </tr>
  117. <tr>
  118. <td valign="top">outputproperty</td>
  119. <td valign="top">The name of a property in which the output of the
  120. command should be stored. If you use the commandResource
  121. attribute, each command's output will be prefixed by the
  122. command itself.</td>
  123. <td align="center" valign="top">No</td>
  124. </tr>
  125. <tr>
  126. <td valign="top">timeout</td>
  127. <td valign="top">Stop the command if it doesn't finish within the
  128. specified time (given in milliseconds <b>unlike telnet, which
  129. expects a timeout in seconds</b>).
  130. Defaults to 0 which means &quot;wait forever&quot;.</td>
  131. <td align="center" valign="top">No</td>
  132. </tr>
  133. <tr>
  134. <td valign="top">input</td>
  135. <td valign="top">A file from which the executed command's standard
  136. input is taken. This attribute is mutually exclusive with the
  137. inputstring attribute.<br/>
  138. When executing more than one command via commandResource, input
  139. will be read for each command.
  140. <em>since Ant 1.8.0</em></td>
  141. <td align="center" valign="top">No</td>
  142. </tr>
  143. <tr>
  144. <td valign="top">verbose</td>
  145. <td valign="top">Determines whether sshexec outputs verbosely to the user.<br/>
  146. Similar output is generated as the ssh commandline tool wit the -v option.
  147. <em>since Ant 1.8.0</em></td>
  148. <td align="center">No, defaults to false</td>
  149. </tr>
  150. <tr>
  151. <td valign="top">inputstring</td>
  152. <td valign="top">A string which serves as the input stream for the
  153. executed command. This attribute is mutually exclusive with the
  154. input attribute.<br/>
  155. When executing more than one command via commandResource, input
  156. will be read for each command.
  157. <em>since Ant 1.8.0</em></td>
  158. <td align="center" valign="top">No</td>
  159. </tr>
  160. </table>
  161. <h3>Examples</h3>
  162. <p><b>Run a command on a remote machine using password authentication</b></p>
  163. <pre>
  164. &lt;sshexec host=&quot;somehost&quot;
  165. username=&quot;dude&quot;
  166. password=&quot;yo&quot;
  167. command=&quot;touch somefile&quot;/&gt;
  168. </pre>
  169. <p><b>Run a command on a remote machine using key authentication</b></p>
  170. <pre>
  171. &lt;sshexec host=&quot;somehost&quot;
  172. username=&quot;dude&quot;
  173. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  174. passphrase=&quot;yo its a secret&quot;
  175. command=&quot;touch somefile&quot;/&gt;
  176. </pre>
  177. <p><b>Run a command on a remote machine using key authentication with no passphrase</b></p>
  178. <pre>
  179. &lt;sshexec host=&quot;somehost&quot;
  180. username=&quot;dude&quot;
  181. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  182. command=&quot;touch somefile&quot;/&gt;
  183. </pre>
  184. <p><b>Run a set of commands from a command resource (file) on a remote machine using key authentication with no passphrase</b></p>
  185. <pre>
  186. &lt;sshexec host=&quot;somehost&quot;
  187. username=&quot;dude&quot;
  188. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  189. commandResource=&quot;to_run&quot;/&gt;
  190. </pre>
  191. <p><strong>Security Note:</strong> Hard coding passwords and/or usernames
  192. in sshexec task can be a serious security hole. Consider using variable
  193. substitution and include the password on the command line. For example:<br>
  194. <pre>
  195. &lt;sshexec host=&quot;somehost&quot;
  196. username=&quot;${username}&quot;
  197. password=&quot;${password}&quot;
  198. command=&quot;touch somefile&quot;/&gt;
  199. </pre>
  200. Invoking ant with the following command line:
  201. <pre>
  202. ant -Dusername=me -Dpassword=mypassword target1 target2
  203. </pre>
  204. Is slightly better, but the username/password is exposed to all users
  205. on an Unix system (via the ps command). The best approach is to use
  206. the
  207. <code>&lt;input&gt;</code> task and/or retrieve the password from a (secured)
  208. .properties file.
  209. </p>
  210. </body>
  211. </html>