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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 id="sshexec">SSHEXEC</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.</p>
  26. <p><strong>Note</strong>: This task depends on external libraries not included
  27. in the Ant distribution. See <a
  28. href="../install.html#librarydependencies">Library Dependencies</a>
  29. for more information. This task has been tested with jsch-0.1.29 and above
  30. and won't work with versions of jsch earlier than
  31. 0.1.28.</p>
  32. <p>See also the <a href="scp.html">scp task</a></p>
  33. <h3>Parameters</h3>
  34. <table>
  35. <tr>
  36. <td valign="top"><b>Attribute</b></td>
  37. <td valign="top"><b>Description</b></td>
  38. <td align="center" valign="top"><b>Required</b></td>
  39. </tr>
  40. <tr>
  41. <td valign="top">host</td>
  42. <td valign="top">The hostname or IP address of the remote host to which you wish to connect.</td>
  43. <td valign="top" align="center">Yes</td>
  44. </tr>
  45. <tr>
  46. <td valign="top">username</td>
  47. <td valign="top">The username on the remote host to which you are connecting.</td>
  48. <td valign="top" align="center">Yes</td>
  49. </tr>
  50. <tr>
  51. <td valign="top">command</td>
  52. <td valign="top">The command to run on the remote host.</td>
  53. <td valign="top" align="center">Either this or commandResource must be set</td>
  54. </tr>
  55. <tr>
  56. <td valign="top">commandResource</td>
  57. <td valign="top">The resource (file) that contains the commands to run on the remote host.
  58. <em>Since Ant 1.7.1</em></td>
  59. <td valign="top" align="center">Either this or command must be set</td>
  60. </tr>
  61. <tr>
  62. <td valign="top">port</td>
  63. <td valign="top">The port to connect to on the remote host.</td>
  64. <td valign="top" align="center">No, defaults to 22.</td>
  65. </tr>
  66. <tr>
  67. <td valign="top">trust</td>
  68. <td valign="top">This trusts all unknown hosts if set to yes/true.<br>
  69. <strong>Note</strong>: If you set this to false (the default), the
  70. host you connect to must be listed in your knownhosts file, this
  71. also implies that the file exists.</td>
  72. <td valign="top" align="center">No, defaults to No.</td>
  73. </tr>
  74. <tr>
  75. <td valign="top">knownhosts</td>
  76. <td valign="top">This sets the known hosts file to use to validate
  77. the identity of the remote host. This must be a SSH2 format file.
  78. SSH1 format is not supported.</td>
  79. <td valign="top" align="center">No, defaults to
  80. ${user.home}/.ssh/known_hosts.</td>
  81. </tr>
  82. <tr>
  83. <td valign="top">failonerror</td>
  84. <td valign="top">Whether to halt the build if the command does not complete successfully.
  85. </td>
  86. <td valign="top" align="center">No; defaults to true.</td>
  87. </tr>
  88. <tr>
  89. <td valign="top">password</td>
  90. <td valign="top">The password.</td>
  91. <td valign="top" align="center">Not if you are using key based
  92. authentication or the password has been given in the file or
  93. todir attribute.</td>
  94. </tr>
  95. <tr>
  96. <td valign="top">keyfile</td>
  97. <td valign="top">Location of the file holding the private key.</td>
  98. <td valign="top" align="center">Yes, if you are using key based
  99. authentication.</td>
  100. </tr>
  101. <tr>
  102. <td valign="top">passphrase</td>
  103. <td valign="top">Passphrase for your private key.</td>
  104. <td valign="top" align="center">No, defaults to an empty string.</td>
  105. </tr>
  106. <tr>
  107. <td valign="top">suppresssystemout</td>
  108. <td valign="top">Whether to suppress system out.
  109. <em>since Ant 1.9.0</em></td>
  110. <td align="center" valign="top">No, defaults to false</td>
  111. </tr>
  112. <tr>
  113. <td valign="top">suppresssystemerr</td>
  114. <td valign="top">Whether to suppress system err.
  115. <em>since Ant 1.9.4</em></td>
  116. <td align="center" valign="top">No, defaults to false</td>
  117. </tr>
  118. <tr>
  119. <td valign="top">output</td>
  120. <td valign="top">Name of a file to which to write the output.</td>
  121. <td align="center" valign="top">No</td>
  122. </tr>
  123. <tr>
  124. <td valign="top">errorOutput</td>
  125. <td valign="top">The file to which the standard error of the
  126. command should be redirected. <em>since Ant 1.9.4</em></td>
  127. <td align="center" valign="top">No</td>
  128. </tr>
  129. <tr>
  130. <td valign="top">append</td>
  131. <td valign="top">Whether output file should be appended to or overwritten. Defaults to false, meaning overwrite any existing file.</td>
  132. <td align="center" valign="top">No</td>
  133. </tr>
  134. <tr>
  135. <td valign="top">errAppend</td>
  136. <td valign="top">Whether errorOutput file should be appended to or
  137. overwritten. Defaults to false, meaning overwrite any existing
  138. file. <em>since Ant 1.9.4</em></td>
  139. <td align="center" valign="top">No</td>
  140. </tr>
  141. <tr>
  142. <td valign="top">outputproperty</td>
  143. <td valign="top">The name of a property in which the output of the
  144. command should be stored. If you use the commandResource
  145. attribute, each command's output will be prefixed by the
  146. command itself.</td>
  147. <td align="center" valign="top">No</td>
  148. </tr>
  149. <tr>
  150. <td valign="top">errorproperty</td>
  151. <td valign="top">The name of a property in which the standard error of the
  152. command should be stored. <em>since Ant 1.9.4</em></td>
  153. <td align="center" valign="top">No</td>
  154. </tr>
  155. <tr>
  156. <td valign="top">resultproperty</td>
  157. <td valign="top">the name of a property in which the return code
  158. of the command should be stored. Only of interest if
  159. failonerror=false. <em>since Ant 1.9.4</em></td>
  160. <td align="center" valign="top">No</td>
  161. </tr>
  162. <tr>
  163. <td valign="top">timeout</td>
  164. <td valign="top">Stop the command if it doesn't finish within the
  165. specified time (given in milliseconds <b>unlike telnet, which
  166. expects a timeout in seconds</b>).
  167. Defaults to 0 which means &quot;wait forever&quot;.</td>
  168. <td align="center" valign="top">No</td>
  169. </tr>
  170. <tr>
  171. <td valign="top">input</td>
  172. <td valign="top">A file from which the executed command's standard
  173. input is taken. This attribute is mutually exclusive with the
  174. inputstring and inputproperty attributes.<br/>
  175. When executing more than one command via commandResource, input
  176. will be read for each command.
  177. <em>since Ant 1.8.0</em></td>
  178. <td align="center" valign="top">No</td>
  179. </tr>
  180. <tr>
  181. <td valign="top">verbose</td>
  182. <td valign="top">Determines whether sshexec outputs verbosely to the user.<br/>
  183. Similar output is generated as the ssh commandline tool wit the -v option.
  184. <em>since Ant 1.8.0</em></td>
  185. <td align="center">No, defaults to false</td>
  186. </tr>
  187. <tr>
  188. <td valign="top">inputproperty</td>
  189. <td valign="top">Name of a property who's content serves as the
  190. input stream for the executed command. This attribute is
  191. mutually exclusive with the input and inputstring
  192. attributes.<br/>
  193. When executing more than one command via commandResource, input
  194. will be read for each command.
  195. <em>since Ant 1.8.0</em></td>
  196. <td align="center" valign="top">No</td>
  197. </tr>
  198. <tr>
  199. <td valign="top">inputstring</td>
  200. <td valign="top">A string which serves as the input stream for the
  201. executed command. This attribute is mutually exclusive with the
  202. input and inputproperty attributes.<br/>
  203. When executing more than one command via commandResource, input
  204. will be read for each command.
  205. <em>since Ant 1.8.3</em></td>
  206. <td align="center" valign="top">No</td>
  207. </tr>
  208. <tr>
  209. <td valign="top">usepty</td>
  210. <td valign="top">Whether to allocate a pseudo-tty (like ssh -t).
  211. <em>since Ant 1.8.3</em></td>
  212. <td align="center" valign="top">No, defaults to false</td>
  213. </tr>
  214. <tr>
  215. <td valign="top">useSystemIn</td>
  216. <td valign="top">Whether to pass the current standard input to the
  217. remote process.
  218. <em>since Ant 1.9.4</em></td>
  219. <td align="center" valign="top">No, defaults to false</td>
  220. </tr>
  221. <tr>
  222. <td valign="top">serverAliveIntervalSeconds</td>
  223. <td valign="top">Sets a timeout interval in seconds after which if no data has
  224. been received from the server, the task will send a message through
  225. the encrypted channel to request a response from the server.
  226. <em>since Ant 1.9.7</em></td>
  227. <td align="center" valign="top">No, the default is 0, indicating
  228. that these messages will not be sent to the server</td>
  229. </tr>
  230. <tr>
  231. <td valign="top">serverAliveCountMax</td>
  232. <td valign="top">The number of server alive messages which may be
  233. sent without receiving any messages back from the server. Only
  234. used if serverAliveIntervalSeconds is not 0.
  235. <em>since Ant 1.9.7</em></td>
  236. <td align="center" valign="top">No, defaults to 3</td>
  237. </tr>
  238. </table>
  239. <h3>Examples</h3>
  240. <p><b>Run a command on a remote machine using password authentication</b></p>
  241. <pre>
  242. &lt;sshexec host=&quot;somehost&quot;
  243. username=&quot;dude&quot;
  244. password=&quot;yo&quot;
  245. command=&quot;touch somefile&quot;/&gt;
  246. </pre>
  247. <p><b>Run a command on a remote machine using key authentication</b></p>
  248. <pre>
  249. &lt;sshexec host=&quot;somehost&quot;
  250. username=&quot;dude&quot;
  251. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  252. passphrase=&quot;yo its a secret&quot;
  253. command=&quot;touch somefile&quot;/&gt;
  254. </pre>
  255. <p><b>Run a command on a remote machine using key authentication with no passphrase</b></p>
  256. <pre>
  257. &lt;sshexec host=&quot;somehost&quot;
  258. username=&quot;dude&quot;
  259. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  260. command=&quot;touch somefile&quot;/&gt;
  261. </pre>
  262. <p><b>Run a set of commands from a command resource (file) on a remote machine using key authentication with no passphrase</b></p>
  263. <pre>
  264. &lt;sshexec host=&quot;somehost&quot;
  265. username=&quot;dude&quot;
  266. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  267. commandResource=&quot;to_run&quot;/&gt;
  268. </pre>
  269. <p><strong>Security Note</strong>: Hard coding passwords and/or usernames
  270. in sshexec task can be a serious security hole. Consider using variable
  271. substitution and include the password on the command line. For example:</p>
  272. <pre>
  273. &lt;sshexec host=&quot;somehost&quot;
  274. username=&quot;${username}&quot;
  275. password=&quot;${password}&quot;
  276. command=&quot;touch somefile&quot;/&gt;
  277. </pre>
  278. Invoking Ant with the following command line:
  279. <pre>
  280. ant -Dusername=me -Dpassword=mypassword target1 target2
  281. </pre>
  282. <p>
  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>