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

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