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 9.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 Apache 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">suppresssystemout</td>
  109. <td valign="top">Whether to suppress system out.
  110. <em>since Ant 1.9.0</em></td>
  111. <td align="center" valign="top">No, defaults to false</td>
  112. </tr>
  113. <tr>
  114. <td valign="top">output</td>
  115. <td valign="top">Name of a file to which to write the output.</td>
  116. <td align="center" valign="top">No</td>
  117. </tr>
  118. <tr>
  119. <td valign="top">append</td>
  120. <td valign="top">Whether output file should be appended to or overwritten. Defaults to false, meaning overwrite any existing file.</td>
  121. <td align="center" valign="top">No</td>
  122. </tr>
  123. <tr>
  124. <td valign="top">outputproperty</td>
  125. <td valign="top">The name of a property in which the output of the
  126. command should be stored. If you use the commandResource
  127. attribute, each command's output will be prefixed by the
  128. command itself.</td>
  129. <td align="center" valign="top">No</td>
  130. </tr>
  131. <tr>
  132. <td valign="top">timeout</td>
  133. <td valign="top">Stop the command if it doesn't finish within the
  134. specified time (given in milliseconds <b>unlike telnet, which
  135. expects a timeout in seconds</b>).
  136. Defaults to 0 which means &quot;wait forever&quot;.</td>
  137. <td align="center" valign="top">No</td>
  138. </tr>
  139. <tr>
  140. <td valign="top">input</td>
  141. <td valign="top">A file from which the executed command's standard
  142. input is taken. This attribute is mutually exclusive with the
  143. inputstring and inputproperty attributes.<br/>
  144. When executing more than one command via commandResource, input
  145. will be read for each command.
  146. <em>since Ant 1.8.0</em></td>
  147. <td align="center" valign="top">No</td>
  148. </tr>
  149. <tr>
  150. <td valign="top">verbose</td>
  151. <td valign="top">Determines whether sshexec outputs verbosely to the user.<br/>
  152. Similar output is generated as the ssh commandline tool wit the -v option.
  153. <em>since Ant 1.8.0</em></td>
  154. <td align="center">No, defaults to false</td>
  155. </tr>
  156. <tr>
  157. <td valign="top">inputproperty</td>
  158. <td valign="top">Name of a property who's content serves as the
  159. input stream for the executed command. This attribute is
  160. mutually exclusive with the input and inputstring
  161. attributes.<br/>
  162. When executing more than one command via commandResource, input
  163. will be read for each command.
  164. <em>since Ant 1.8.0</em></td>
  165. <td align="center" valign="top">No</td>
  166. </tr>
  167. <tr>
  168. <td valign="top">inputstring</td>
  169. <td valign="top">A string which serves as the input stream for the
  170. executed command. This attribute is mutually exclusive with the
  171. input and inputproperty attributes.<br/>
  172. When executing more than one command via commandResource, input
  173. will be read for each command.
  174. <em>since Ant 1.8.3</em></td>
  175. <td align="center" valign="top">No</td>
  176. </tr>
  177. <tr>
  178. <td valign="top">usepty</td>
  179. <td valign="top">Whether to allocate a pseudo-tty (like ssh -t).
  180. <em>since Ant 1.8.3</em></td>
  181. <td align="center" valign="top">No, defaults to false</td>
  182. </tr>
  183. </table>
  184. <h3>Examples</h3>
  185. <p><b>Run a command on a remote machine using password authentication</b></p>
  186. <pre>
  187. &lt;sshexec host=&quot;somehost&quot;
  188. username=&quot;dude&quot;
  189. password=&quot;yo&quot;
  190. command=&quot;touch somefile&quot;/&gt;
  191. </pre>
  192. <p><b>Run a command on a remote machine using key authentication</b></p>
  193. <pre>
  194. &lt;sshexec host=&quot;somehost&quot;
  195. username=&quot;dude&quot;
  196. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  197. passphrase=&quot;yo its a secret&quot;
  198. command=&quot;touch somefile&quot;/&gt;
  199. </pre>
  200. <p><b>Run a command on a remote machine using key authentication with no passphrase</b></p>
  201. <pre>
  202. &lt;sshexec host=&quot;somehost&quot;
  203. username=&quot;dude&quot;
  204. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  205. command=&quot;touch somefile&quot;/&gt;
  206. </pre>
  207. <p><b>Run a set of commands from a command resource (file) on a remote machine using key authentication with no passphrase</b></p>
  208. <pre>
  209. &lt;sshexec host=&quot;somehost&quot;
  210. username=&quot;dude&quot;
  211. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  212. commandResource=&quot;to_run&quot;/&gt;
  213. </pre>
  214. <p><strong>Security Note:</strong> Hard coding passwords and/or usernames
  215. in sshexec task can be a serious security hole. Consider using variable
  216. substitution and include the password on the command line. For example:<br>
  217. <pre>
  218. &lt;sshexec host=&quot;somehost&quot;
  219. username=&quot;${username}&quot;
  220. password=&quot;${password}&quot;
  221. command=&quot;touch somefile&quot;/&gt;
  222. </pre>
  223. Invoking ant with the following command line:
  224. <pre>
  225. ant -Dusername=me -Dpassword=mypassword target1 target2
  226. </pre>
  227. Is slightly better, but the username/password is exposed to all users
  228. on an Unix system (via the ps command). The best approach is to use
  229. the
  230. <code>&lt;input&gt;</code> task and/or retrieve the password from a (secured)
  231. .properties file.
  232. </p>
  233. </body>
  234. </html>