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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <!DOCTYPE html>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. https://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. <html lang="en">
  17. <head>
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>SSHEXEC Task</title>
  20. </head>
  21. <body>
  22. <h2 id="sshexec">SSHEXEC</h2>
  23. <p><em>Since Apache Ant 1.6</em></p>
  24. <h3>Description</h3>
  25. <p>Runs a command on a remote machine running SSH daemon.</p>
  26. <p><strong>Note</strong>: This task depends on external libraries not included in the Ant
  27. distribution. See <a href="../install.html#librarydependencies">Library Dependencies</a> for more
  28. information. This task has been tested with JSCh 0.1.29 and above and won't work with versions of
  29. JSCh earlier than 0.1.28.</p>
  30. <p>See also the <a href="scp.html">scp task</a></p>
  31. <h3>Parameters</h3>
  32. <table class="attr">
  33. <tr>
  34. <th scope="col">Attribute</th>
  35. <th scope="col">Description</th>
  36. <th scope="col">Required</th>
  37. </tr>
  38. <tr>
  39. <td>host</td>
  40. <td>The hostname or IP address of the remote host to which you wish to connect.</td>
  41. <td>Yes</td>
  42. </tr>
  43. <tr>
  44. <td>username</td>
  45. <td>The username on the remote host to which you are connecting.</td>
  46. <td>Yes</td>
  47. </tr>
  48. <tr>
  49. <td>command</td>
  50. <td>The command to run on the remote host.</td>
  51. <td rowspan="2">Exactly one of the two</td>
  52. </tr>
  53. <tr>
  54. <td>commandResource</td>
  55. <td class="left">The resource (file) that contains the commands to run on the remote
  56. host. <em>Since Ant 1.7.1</em></td>
  57. </tr>
  58. <tr>
  59. <td>port</td>
  60. <td>The port to connect to on the remote host.</td>
  61. <td>No; defaults to <q>22</q></td>
  62. </tr>
  63. <tr>
  64. <td>trust</td>
  65. <td>This trusts all unknown hosts if set to <q>yes</q>
  66. or <q>true</q>.<br/><strong>Note</strong>: If you set this to <q>false</q> (the default), the
  67. host you connect to must be listed in your <var>knownhosts</var> file, this also implies that
  68. the file exists.</td>
  69. <td>No; defaults to <q>no</q></td>
  70. </tr>
  71. <tr>
  72. <td>knownhosts</td>
  73. <td>This sets the known hosts file to use to validate the identity of the remote host. This
  74. must be a SSH2 format file. SSH1 format is not supported.</td>
  75. <td>No; defaults to <q>${user.home}/.ssh/known_hosts</q></td>
  76. </tr>
  77. <tr>
  78. <td>failonerror</td>
  79. <td>Whether to halt the build if the command does not complete successfully.</td>
  80. <td>No; defaults to <q>true</q></td>
  81. </tr>
  82. <tr>
  83. <td>password</td>
  84. <td>The password.</td>
  85. <td>Yes, unless you are using key based authentication or the password has been given in the
  86. file or <var>todir</var> attribute.</td>
  87. </tr>
  88. <tr>
  89. <td>keyfile</td>
  90. <td>Location of the file holding the private key.</td>
  91. <td>Yes, if you are using key based authentication</td>
  92. </tr>
  93. <tr>
  94. <td>passphrase</td>
  95. <td>Passphrase for your private key.</td>
  96. <td>No; defaults to an empty string</td>
  97. </tr>
  98. <tr>
  99. <td>sshConfig</td>
  100. <td>Location of the file holding the OpenSSH style configuration (e.g. <code>${user.home}/.ssh/config</code>).
  101. The username and the key file are read from the configuration file,
  102. unless they are already specified in the task parameters.
  103. <em>since Ant 1.10.8</em></td>
  104. <td>No</td>
  105. </tr>
  106. <tr>
  107. <td>suppresssystemout</td>
  108. <td>Whether to suppress system out. <em>since Ant 1.9.0</em></td>
  109. <td>No; defaults to <q>false</q></td>
  110. </tr>
  111. <tr>
  112. <td>suppresssystemerr</td>
  113. <td>Whether to suppress <code>System.err</code>. <em>since Ant 1.9.4</em></td>
  114. <td>No; defaults to <q>false</q></td>
  115. </tr>
  116. <tr>
  117. <td>output</td>
  118. <td>Name of a file to which to write the output.</td>
  119. <td>No</td>
  120. </tr>
  121. <tr>
  122. <td>errorOutput</td>
  123. <td>The file to which the standard error of the command should be redirected. <em>since Ant
  124. 1.9.4</em></td>
  125. <td>No</td>
  126. </tr>
  127. <tr>
  128. <td>append</td>
  129. <td>Whether <var>output</var> file should be appended to or overwritten.</td>
  130. <td>No; defaults to <q>false</q>, meaning overwrite any existing file</td>
  131. </tr>
  132. <tr>
  133. <td>errAppend</td>
  134. <td>Whether <var>errorOutput</var> file should be appended to or overwritten. <em>since Ant
  135. 1.9.4</em></td>
  136. <td>No; defaults to <q>false</q>, meaning overwrite any existing file</td>
  137. </tr>
  138. <tr>
  139. <td>outputproperty</td>
  140. <td>The name of a property in which the output of the command should be stored. If you use
  141. the <var>commandResource</var> attribute, each command's output will be prefixed by the
  142. command itself.</td>
  143. <td>No</td>
  144. </tr>
  145. <tr>
  146. <td>errorproperty</td>
  147. <td>The name of a property in which the standard error of the command should be
  148. stored. <em>since Ant 1.9.4</em></td>
  149. <td>No</td>
  150. </tr>
  151. <tr>
  152. <td>resultproperty</td>
  153. <td>the name of a property in which the return code of the command should be stored. Only of
  154. interest if <var>failonerror</var>=<q>false</q>. <em>since Ant 1.9.4</em></td>
  155. <td>No</td>
  156. </tr>
  157. <tr>
  158. <td>timeout</td>
  159. <td>Stop the command if it doesn't finish within the specified time (given in
  160. milliseconds <strong>unlike <code>telnet</code>, which expects a timeout in
  161. seconds</strong>).</td>
  162. <td>No; defaults to <q>0</q> which means <q>never</q></td>
  163. </tr>
  164. <tr>
  165. <td>input</td>
  166. <td>A file from which the executed command's standard input is taken. This attribute is mutually
  167. exclusive with the <var>inputstring</var> and <var>inputproperty</var> attributes.<br/> When
  168. executing more than one command via <var>commandResource</var>, input will be read for each
  169. command. <em>since Ant 1.8.0</em></td>
  170. <td>No</td>
  171. </tr>
  172. <tr>
  173. <td>verbose</td>
  174. <td>Determines whether <code>sshexec</code> outputs verbosely to the user.<br/> Similar output
  175. is generated as the <kbd>ssh</kbd> command line tool with the <kbd>-v</kbd>
  176. option. <em>since Ant 1.8.0</em></td>
  177. <td>No; defaults to <q>false</q></td>
  178. </tr>
  179. <tr>
  180. <td>inputproperty</td>
  181. <td>Name of a property content of which serves as the input stream for the executed
  182. command. This attribute is mutually exclusive with the <var>input</var>
  183. and <var>inputstring</var> attributes.<br/> When executing more than one command
  184. via <var>commandResource</var>, input will be read for each command. <em>since Ant
  185. 1.8.0</em></td>
  186. <td>No</td>
  187. </tr>
  188. <tr>
  189. <td>inputstring</td>
  190. <td>A string which serves as the input stream for the executed command. This attribute is
  191. mutually exclusive with the <var>input</var> and <var>inputproperty</var> attributes.<br/>
  192. When executing more than one command via <var>commandResource</var>, input will be read for
  193. each command. <em>since Ant 1.8.3</em></td>
  194. <td>No</td>
  195. </tr>
  196. <tr>
  197. <td>usepty</td>
  198. <td>Whether to allocate a pseudo-tty (like <kbd>ssh -t</kbd>). <em>since Ant 1.8.3</em></td>
  199. <td>No; defaults to <q>false</q></td>
  200. </tr>
  201. <tr>
  202. <td>useSystemIn</td>
  203. <td>Whether to pass the current standard input to the remote process. <em>since Ant
  204. 1.9.4</em></td>
  205. <td>No; defaults to <q>false</q></td>
  206. </tr>
  207. <tr>
  208. <td>serverAliveIntervalSeconds</td>
  209. <td>Sets a timeout interval in seconds after which if no data has been received from the server,
  210. the task will send a message through the encrypted channel to request a response from the
  211. server. <em>since Ant 1.9.7</em></td>
  212. <td>No, the default is <q>0</q>, indicating that these messages will not be sent to the
  213. server</td>
  214. </tr>
  215. <tr>
  216. <td>serverAliveCountMax</td>
  217. <td>The number of server alive messages which may be sent without receiving any messages back
  218. from the server. Only used if <var>serverAliveIntervalSeconds</var> is
  219. not <q>0</q>. <em>since Ant 1.9.7</em></td>
  220. <td>No; defaults to <q>3</q></td>
  221. </tr>
  222. </table>
  223. <h3>Parameters specified as nested elements</h3>
  224. <h4 id="additionalConfig">additionalConfig</h4>
  225. <p><em>since Ant 1.10.10</em></p>
  226. <p>Adds configuration settings for the JSch Session created that are
  227. not directly supported by specific Ant attributes.</p>
  228. <table class="attr">
  229. <tr>
  230. <th scope="col">Attribute</th>
  231. <th scope="col">Description</th>
  232. <th scope="col">Required</th>
  233. </tr>
  234. <tr>
  235. <td>key</td>
  236. <td>The key of the configuration setting.</td>
  237. <td>Yes</td>
  238. </tr>
  239. <tr>
  240. <td>value</td>
  241. <td>The value of the configuration setting.</td>
  242. <td>Yes</td>
  243. </tr>
  244. </table>
  245. <h3>Examples</h3>
  246. <p>Run a command on a remote machine using password authentication</p>
  247. <pre>
  248. &lt;sshexec host=&quot;somehost&quot;
  249. username=&quot;dude&quot;
  250. password=&quot;yo&quot;
  251. command=&quot;touch somefile&quot;/&gt;</pre>
  252. <p>Run a command on a remote machine using key authentication</p>
  253. <pre>
  254. &lt;sshexec host=&quot;somehost&quot;
  255. username=&quot;dude&quot;
  256. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  257. passphrase=&quot;yo its a secret&quot;
  258. command=&quot;touch somefile&quot;/&gt;</pre>
  259. <p>Run a command on a remote machine using key authentication with no passphrase</p>
  260. <pre>
  261. &lt;sshexec host=&quot;somehost&quot;
  262. username=&quot;dude&quot;
  263. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  264. command=&quot;touch somefile&quot;/&gt;</pre>
  265. <p>Run a set of commands from a command resource (file) on a remote machine using key authentication
  266. with no passphrase</p>
  267. <pre>
  268. &lt;sshexec host=&quot;somehost&quot;
  269. username=&quot;dude&quot;
  270. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  271. commandResource=&quot;to_run&quot;/&gt;</pre>
  272. <p><strong>Security Note</strong>: Hardcoding passwords and/or usernames in <var>sshexec</var> task
  273. can be a serious security hole. Consider using variable substitution and include the password on the
  274. command line. For example:</p>
  275. <pre>
  276. &lt;sshexec host=&quot;somehost&quot;
  277. username=&quot;${username}&quot;
  278. password=&quot;${password}&quot;
  279. command=&quot;touch somefile&quot;/&gt;</pre>
  280. <p>Invoking Ant with the following command line:</p>
  281. <pre class="input">ant -Dusername=me -Dpassword=mypassword target1 target2</pre>
  282. <p>is slightly better, but the username/password is exposed to all users on an Unix system (via
  283. the <kbd>ps</kbd> command). The best approach is to use the <code>&lt;input&gt;</code> task and/or
  284. retrieve the password from a (secured) <samp>.properties</samp> file.</p>
  285. </body>
  286. </html>