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 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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">suppresssystemerr</td>
  115. <td valign="top">Whether to suppress system err.
  116. <em>since Ant 1.9.4</em></td>
  117. <td align="center" valign="top">No, defaults to false</td>
  118. </tr>
  119. <tr>
  120. <td valign="top">output</td>
  121. <td valign="top">Name of a file to which to write the output.</td>
  122. <td align="center" valign="top">No</td>
  123. </tr>
  124. <tr>
  125. <td valign="top">errorOutput</td>
  126. <td valign="top">The file to which the standard error of the
  127. command should be redirected. <em>since Ant 1.9.4</em></td>
  128. <td align="center" valign="top">No</td>
  129. </tr>
  130. <tr>
  131. <td valign="top">append</td>
  132. <td valign="top">Whether output file should be appended to or overwritten. Defaults to false, meaning overwrite any existing file.</td>
  133. <td align="center" valign="top">No</td>
  134. </tr>
  135. <tr>
  136. <td valign="top">errAppend</td>
  137. <td valign="top">Whether errorOutput file should be appended to or
  138. overwritten. Defaults to false, meaning overwrite any existing
  139. file. <em>since Ant 1.9.4</em></td>
  140. <td align="center" valign="top">No</td>
  141. </tr>
  142. <tr>
  143. <td valign="top">outputproperty</td>
  144. <td valign="top">The name of a property in which the output of the
  145. command should be stored. If you use the commandResource
  146. attribute, each command's output will be prefixed by the
  147. command itself.</td>
  148. <td align="center" valign="top">No</td>
  149. </tr>
  150. <tr>
  151. <td valign="top">errorproperty</td>
  152. <td valign="top">The name of a property in which the standard error of the
  153. command should be stored. <em>since Ant 1.9.4</em></td>
  154. <td align="center" valign="top">No</td>
  155. </tr>
  156. <tr>
  157. <td valign="top">resultproperty</td>
  158. <td valign="top">the name of a property in which the return code
  159. of the command should be stored. Only of interest if
  160. failonerror=false. <em>since Ant 1.9.4</em></td>
  161. <td align="center" valign="top">No</td>
  162. </tr>
  163. <tr>
  164. <td valign="top">timeout</td>
  165. <td valign="top">Stop the command if it doesn't finish within the
  166. specified time (given in milliseconds <b>unlike telnet, which
  167. expects a timeout in seconds</b>).
  168. Defaults to 0 which means &quot;wait forever&quot;.</td>
  169. <td align="center" valign="top">No</td>
  170. </tr>
  171. <tr>
  172. <td valign="top">input</td>
  173. <td valign="top">A file from which the executed command's standard
  174. input is taken. This attribute is mutually exclusive with the
  175. inputstring and inputproperty attributes.<br/>
  176. When executing more than one command via commandResource, input
  177. will be read for each command.
  178. <em>since Ant 1.8.0</em></td>
  179. <td align="center" valign="top">No</td>
  180. </tr>
  181. <tr>
  182. <td valign="top">verbose</td>
  183. <td valign="top">Determines whether sshexec outputs verbosely to the user.<br/>
  184. Similar output is generated as the ssh commandline tool wit the -v option.
  185. <em>since Ant 1.8.0</em></td>
  186. <td align="center">No, defaults to false</td>
  187. </tr>
  188. <tr>
  189. <td valign="top">inputproperty</td>
  190. <td valign="top">Name of a property who's content serves as the
  191. input stream for the executed command. This attribute is
  192. mutually exclusive with the input and inputstring
  193. attributes.<br/>
  194. When executing more than one command via commandResource, input
  195. will be read for each command.
  196. <em>since Ant 1.8.0</em></td>
  197. <td align="center" valign="top">No</td>
  198. </tr>
  199. <tr>
  200. <td valign="top">inputstring</td>
  201. <td valign="top">A string which serves as the input stream for the
  202. executed command. This attribute is mutually exclusive with the
  203. input and inputproperty attributes.<br/>
  204. When executing more than one command via commandResource, input
  205. will be read for each command.
  206. <em>since Ant 1.8.3</em></td>
  207. <td align="center" valign="top">No</td>
  208. </tr>
  209. <tr>
  210. <td valign="top">usepty</td>
  211. <td valign="top">Whether to allocate a pseudo-tty (like ssh -t).
  212. <em>since Ant 1.8.3</em></td>
  213. <td align="center" valign="top">No, defaults to false</td>
  214. </tr>
  215. <tr>
  216. <td valign="top">useSystemIn</td>
  217. <td valign="top">Whether to pass the current standard input to the
  218. remote process.
  219. <em>since Ant 1.9.4</em></td>
  220. <td align="center" valign="top">No, defaults to false</td>
  221. </tr>
  222. <tr>
  223. <td valign="top">serverAliveIntervalSeconds</td>
  224. <td valign="top">Sets a timeout interval in seconds after which if no data has
  225. been received from the server, the task will send a message through
  226. the encrypted channel to request a response from the server.
  227. <em>since Ant 1.9.7</em></td>
  228. <td align="center" valign="top">No, the default is 0, indicating
  229. that these messages will not be sent to the server</td>
  230. </tr>
  231. <tr>
  232. <td valign="top">serverAliveCountMax</td>
  233. <td valign="top">The number of server alive messages which may be
  234. sent without receiving any messages back from the server. Only
  235. used if serverAliveIntervalSeconds is not 0.
  236. <em>since Ant 1.9.7</em></td>
  237. <td align="center" valign="top">No, defaults to 3</td>
  238. </tr>
  239. </table>
  240. <h3>Examples</h3>
  241. <p><b>Run a command on a remote machine using password authentication</b></p>
  242. <pre>
  243. &lt;sshexec host=&quot;somehost&quot;
  244. username=&quot;dude&quot;
  245. password=&quot;yo&quot;
  246. command=&quot;touch somefile&quot;/&gt;
  247. </pre>
  248. <p><b>Run a command on a remote machine using key authentication</b></p>
  249. <pre>
  250. &lt;sshexec host=&quot;somehost&quot;
  251. username=&quot;dude&quot;
  252. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  253. passphrase=&quot;yo its a secret&quot;
  254. command=&quot;touch somefile&quot;/&gt;
  255. </pre>
  256. <p><b>Run a command on a remote machine using key authentication with no passphrase</b></p>
  257. <pre>
  258. &lt;sshexec host=&quot;somehost&quot;
  259. username=&quot;dude&quot;
  260. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  261. command=&quot;touch somefile&quot;/&gt;
  262. </pre>
  263. <p><b>Run a set of commands from a command resource (file) on a remote machine using key authentication with no passphrase</b></p>
  264. <pre>
  265. &lt;sshexec host=&quot;somehost&quot;
  266. username=&quot;dude&quot;
  267. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  268. commandResource=&quot;to_run&quot;/&gt;
  269. </pre>
  270. <p><strong>Security Note:</strong> Hard coding passwords and/or usernames
  271. in sshexec task can be a serious security hole. Consider using variable
  272. substitution and include the password on the command line. For example:<br>
  273. <pre>
  274. &lt;sshexec host=&quot;somehost&quot;
  275. username=&quot;${username}&quot;
  276. password=&quot;${password}&quot;
  277. command=&quot;touch somefile&quot;/&gt;
  278. </pre>
  279. Invoking ant with the following command line:
  280. <pre>
  281. ant -Dusername=me -Dpassword=mypassword target1 target2
  282. </pre>
  283. Is slightly better, but the username/password is exposed to all users
  284. on an Unix system (via the ps command). The best approach is to use
  285. the
  286. <code>&lt;input&gt;</code> task and/or retrieve the password from a (secured)
  287. .properties file.
  288. </p>
  289. </body>
  290. </html>