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.

sshsession.html 9.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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>SSHSESSION Task</title>
  20. </head>
  21. <body>
  22. <h2 id="sshsession">SSHSESSION</h2>
  23. <p><em>Since Apache Ant 1.8.0</em></p>
  24. <h3>Description</h3>
  25. <p>A Task which establishes an SSH connection with a remote machine running SSH daemon, optionally
  26. establishes any number of local or remote tunnels over that connection, then executes any nested
  27. tasks before taking down the connection.</p>
  28. <p><strong>Note</strong>: This task depends on external libraries not included in the Ant
  29. distribution. See <a href="../install.html#librarydependencies">Library Dependencies</a> for more
  30. information. This task has been tested with JSCh 0.1.33 and above and won't work with versions of
  31. JSCh earlier than 0.1.28.</p>
  32. <p>See also the <a href="sshexec.html">sshexec</a> and <a href="scp.html">scp</a> tasks</p>
  33. <h3>Parameters</h3>
  34. <table class="attr">
  35. <tr>
  36. <th scope="col">Attribute</th>
  37. <th scope="col">Description</th>
  38. <th scope="col">Required</th>
  39. </tr>
  40. <tr>
  41. <td>host</td>
  42. <td>The hostname or IP address of the remote host to which you wish to connect.</td>
  43. <td>Yes</td>
  44. </tr>
  45. <tr>
  46. <td>username</td>
  47. <td>The username on the remote host to which you are connecting.</td>
  48. <td>Yes</td>
  49. </tr>
  50. <tr>
  51. <td>port</td>
  52. <td>The port to connect to on the remote host.</td>
  53. <td>No; defaults to <q>22</q></td>
  54. </tr>
  55. <tr>
  56. <td>localtunnels</td>
  57. <td>A comma-delimited list of colon-delimited <samp>lport:rhost:rport</samp> triplets defining
  58. local port forwarding.<br/>If nested <a href="#LocalTunnel">localtunnel</a> elements are also
  59. provided, both sets of tunnels will be established.</td>
  60. <td>No</td>
  61. </tr>
  62. <tr>
  63. <td>remotetunnels</td>
  64. <td>A comma-delimited list of colon-delimited <samp>rport:lhost:lport</samp> triplets defining
  65. remote port forwarding.<br/> If nested <a href="#RemoteTunnel">remotetunnel</a> elements are
  66. also provided, both sets of tunnels will be established.</td>
  67. <td>No</td>
  68. </tr>
  69. <tr>
  70. <td>trust</td>
  71. <td>This trusts all unknown hosts if set to <q>yes</q>
  72. or <q>true</q>.<br/><strong>Note</strong>: If you set this to <q>false</q> (the default), the
  73. host you connect to must be listed in your <var>knownhosts</var> file, this also implies that
  74. the file exists.</td>
  75. <td>No; defaults to <q>no</q></td>
  76. </tr>
  77. <tr>
  78. <td>knownhosts</td>
  79. <td>This sets the known hosts file to use to validate the identity of the remote host. This must
  80. be a SSH2 format file. SSH1 format is not supported.</td>
  81. <td>No; defaults to <q>${user.home}/.ssh/known_hosts</q></td>
  82. </tr>
  83. <tr>
  84. <td>failonerror</td>
  85. <td>Whether to halt the build if the command does not complete successfully.</td>
  86. <td>No; defaults to <q>true</q></td>
  87. </tr>
  88. <tr>
  89. <td>password</td>
  90. <td>The password.</td>
  91. <td>Yes, unless you are using key based authentication or the password has been given in the
  92. file or <var>todir</var> attribute</td>
  93. </tr>
  94. <tr>
  95. <td>keyfile</td>
  96. <td>Location of the file holding the private key.</td>
  97. <td>Yes, if you are using key based authentication</td>
  98. </tr>
  99. <tr>
  100. <td>passphrase</td>
  101. <td>Passphrase for your private key.</td>
  102. <td>No; defaults to an empty string</td>
  103. </tr>
  104. <tr>
  105. <td>sshConfig</td>
  106. <td>Location of the file holding the OpenSSH style configuration (e.g. <code>${user.home}/.ssh/config</code>).
  107. The username and the key file are read from the configuration file,
  108. unless they are already specified in the task parameters.
  109. <em>since Ant 1.10.8</em></td>
  110. <td>No</td>
  111. </tr>
  112. <tr>
  113. <td>timeout</td>
  114. <td>Give up if the connection cannot be established within the specified time (given in
  115. milliseconds).</td>
  116. <td>No; defaults to <q>0</q> which means <q>never</q></td>
  117. </tr>
  118. </table>
  119. <h3>Parameters specified as nested elements</h3>
  120. <h4 id="LocalTunnel">localtunnel</h4>
  121. <p>Optionally, any number of <code>localtunnel</code> elements can be used to define local port
  122. forwarding over the SSH connection. If the <var>localtunnels</var> parameter was also specified,
  123. both sets of tunnels will be established.</p>
  124. <table class="attr">
  125. <tr>
  126. <th scope="col">Attribute</th>
  127. <th scope="col">Description</th>
  128. <th scope="col">Required</th>
  129. </tr>
  130. <tr>
  131. <td>lport</td>
  132. <td>The number of the local port to be forwarded.</td>
  133. <td>Yes</td>
  134. </tr>
  135. <tr>
  136. <td>rhost</td>
  137. <td>The hostname or IP address of the remote host to which the local port should be
  138. forwarded.</td>
  139. <td>Yes</td>
  140. </tr>
  141. <tr>
  142. <td>rport</td>
  143. <td>The number of the port on the remote host to which the local port should be forwarded.</td>
  144. <td>Yes</td>
  145. </tr>
  146. </table>
  147. <h4 id="RemoteTunnel">remotetunnel</h4>
  148. <p>Optionally, any number of <code>remotetunnel</code> elements can be used to define remote port
  149. forwarding over the SSH connection. If the <var>remotetunnels</var> parameter was also specified,
  150. both sets of tunnels will be established.</p>
  151. <table class="attr">
  152. <tr>
  153. <th scope="col">Attribute</th>
  154. <th scope="col">Description</th>
  155. <th scope="col">Required</th>
  156. </tr>
  157. <tr>
  158. <td>rport</td>
  159. <td>The number of the remote port to be forwarded.</td>
  160. <td>Yes</td>
  161. </tr>
  162. <tr>
  163. <td>lhost</td>
  164. <td>The hostname or IP address of the local host to which the remote port should be
  165. forwarded.</td>
  166. <td>Yes</td>
  167. </tr>
  168. <tr>
  169. <td>lport</td>
  170. <td>The number of the port on the local host to which the remote port should be forwarded.</td>
  171. <td>Yes</td>
  172. </tr>
  173. </table>
  174. <h4 id="Sequential">sequential</h4>
  175. <p>The <code>sequential</code> element is a required parameter. It is a container for nested Tasks
  176. which are to be executed once the SSH connection is established and all local and/or remote tunnels
  177. established.</p>
  178. <h3>Examples</h3>
  179. <p>Connect to a remote machine using password authentication, forward the local CVS port to the
  180. remote host, and execute a CVS command locally, which can use the tunnel.</p>
  181. <pre>
  182. &lt;sshsession host=&quot;somehost&quot;
  183. username=&quot;dude&quot;
  184. password=&quot;yo&quot;
  185. localtunnels=&quot;2401:localhost:2401&quot;&gt;
  186. &lt;sequential&gt;
  187. &lt;cvs command=&quot;update ${cvs.parms} ${module}&quot;
  188. cvsRoot=&quot;${cvs.root}&quot;
  189. dest=&quot;${local.root}&quot;
  190. failonerror=&quot;true&quot;/&gt;
  191. &lt;/sequential&gt;
  192. &lt;/sshsession&gt;</pre>
  193. <p>Do the same thing using nested <code>localtunnel</code> element.</p>
  194. <pre>
  195. &lt;sshsession host=&quot;somehost&quot;
  196. username=&quot;dude&quot;
  197. password=&quot;yo&quot;&gt;
  198. &lt;localtunnel lport=&quot;2401&quot; rhost=&quot;localhost&quot; rport=&quot;2401&quot;/&gt;
  199. &lt;sequential&gt;
  200. &lt;cvs command=&quot;update ${cvs.parms} ${module}&quot;
  201. cvsRoot=&quot;${cvs.root}&quot;
  202. dest=&quot;${local.root}&quot;
  203. failonerror=&quot;true&quot;/&gt;
  204. &lt;/sequential&gt;
  205. &lt;/sshsession&gt;</pre>
  206. <p>Connect to a remote machine using key authentication, forward port 1080 to port 80 of an intranet
  207. server which is not directly accessible, then run a <code>get</code> task using that tunnel.</p>
  208. <pre>
  209. &lt;sshsession host=&quot;somehost&quot;
  210. username=&quot;dude&quot;
  211. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  212. passphrase=&quot;yo its a secret&quot;/&gt;
  213. &lt;LocalTunnel lport=&quot;1080&quot; rhost=&quot;intranet.mycomp.com&quot; rport=&quot;80&quot;/&gt;
  214. &lt;sequential&gt;
  215. &lt;get src=&quot;http://localhost:1080/somefile&quot; dest=&quot;temp/somefile&quot;/&gt;
  216. &lt;/sequential&gt;
  217. &lt;/sshsession&gt;</pre>
  218. <p><strong>Security Note</strong>: Hardcoding passwords or passphrases and/or usernames
  219. in <code>sshsession</code> task can be a serious security hole. Consider using variable substitution
  220. and include the password on the command line. For example:</p>
  221. <pre>
  222. &lt;sshsession host=&quot;somehost&quot;
  223. username=&quot;${username}&quot;
  224. password=&quot;${password}&quot;
  225. localtunnels=&quot;2401:localhost:2401&quot;&gt;
  226. &lt;sequential&gt;
  227. &lt;sometask/&gt;
  228. &lt;/sequential&gt;
  229. &lt;/sshsession&gt;</pre>
  230. <p>Invoking Ant with the following command line:</p>
  231. <pre class="input">ant -Dusername=me -Dpassword=mypassword target1 target2</pre>
  232. <p>is slightly better, but the username/password is exposed to all users on an Unix system (via
  233. the <kbd>ps</kbd> command). The best approach is to use the <code>&lt;input&gt;</code> task and/or
  234. retrieve the password from a (secured) <samp>.properties</samp> file.</p>
  235. </body>
  236. </html>