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.

scp.html 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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>SCP Task</title>
  20. </head>
  21. <body>
  22. <h2 id="scp">SCP</h2>
  23. <p><em>Since Apache Ant 1.6</em></p>
  24. <h3>Description</h3>
  25. <p>Copies a file or FileSet to or from a (remote) machine running an SSH daemon.
  26. FileSet <em>only</em> works for copying files from the local machine to a remote machine.</p>
  27. <p><strong>Note</strong>: This task depends on external libraries not included in the Ant
  28. distribution. See <a href="../install.html#librarydependencies">Library Dependencies</a> for more
  29. information. This task has been tested with <code>jsch-0.1.2</code> and later.</p>
  30. <p>See also the <a href="sshexec.html">sshexec 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>file</td>
  40. <td>The file to copy. This can be a local path or a remote path of the
  41. form <samp>user[:password]@host:/directory/path</samp>. <samp>:password</samp> can be omitted
  42. if you use key based authentication or specify the <var>password</var> attribute. The way
  43. remote path is recognized is whether it contains <q>@</q> character or not. This will not work
  44. if your <var>localPath</var> contains <q>@</q> character.</td>
  45. <td>Yes, unless a nested <code>&lt;fileset&gt;</code> element is used</td>
  46. </tr>
  47. <tr>
  48. <td>localFile</td>
  49. <td>This is an alternative to the <var>file</var> attribute. But this must always point to a
  50. local file. The reason this was added was that when you give file attribute it is treated as
  51. remote if it contains <q>@</q> character. This character can exist also in local
  52. paths. <em>since Ant 1.6.2</em></td>
  53. <td>Alternative to <var>file</var> attribute</td>
  54. </tr>
  55. <tr>
  56. <td>remoteFile</td>
  57. <td>This is an alternative to the <var>file</var> attribute. But this must always point to a
  58. remote file. <em>since Ant 1.6.2</em></td>
  59. <td>Alternative to <var>file</var> attribute</td>
  60. </tr>
  61. <tr>
  62. <td>todir</td>
  63. <td>The directory to copy to. This can be a local path or a remote path of the
  64. form <samp>user[:password]@host:/directory/path</samp>. <samp>:password</samp> can be omitted
  65. if you use key based authentication or specify the <var>password</var> attribute. The way
  66. remote path is recognized is whether it contains <q>@</q> character or not. This will not work
  67. if your <var>localPath</var> contains <q>@</q> character.</td>
  68. <td>Yes</td>
  69. </tr>
  70. <tr>
  71. <td>localTodir</td>
  72. <td>This is an alternative to the <var>todir</var> attribute. But this must always point to a
  73. local directory. The reason this was added was that when you give <var>todir</var> attribute it
  74. is treated as remote if it contains <q>@</q> character. This character can exist also in local
  75. paths. <em>since Ant 1.6.2</em></td>
  76. <td>Alternative to <var>todir</var> attribute</td>
  77. </tr>
  78. <tr>
  79. <td>localTofile</td>
  80. <td>Changes the file name to the given name while receiving it, only useful if receiving a
  81. single file. <em>since Ant 1.6.2</em></td>
  82. <td>Alternative to <var>todir</var> attribute</td>
  83. </tr>
  84. <tr>
  85. <td>remoteTodir</td>
  86. <td>This is an alternative to the <var>todir</var> attribute. But this must always point to a
  87. remote directory. <em>since Ant 1.6.2</em></td>
  88. <td>Alternative to <var>todir</var> attribute</td>
  89. </tr>
  90. <tr>
  91. <td>remoteTofile</td>
  92. <td>Changes the file name to the given name while sending it, only useful if sending a single
  93. file. <em>since Ant 1.6.2</em></td>
  94. <td>Alternative to <var>todir</var> attribute</td>
  95. </tr>
  96. <tr>
  97. <td>port</td>
  98. <td>The port to connect to on the remote host.</td>
  99. <td>No; defaults to <q>22</q></td>
  100. </tr>
  101. <tr>
  102. <td>trust</td>
  103. <td>This trusts all unknown hosts if set to <q>yes</q>
  104. or <q>true</q>.<br/><strong>Note</strong>: If you set this to <q>false</q> (the default), the
  105. host you connect to must be listed in your <var>knownhosts</var> file, this also implies that
  106. the file exists.</td>
  107. <td>No; defaults to <q>no</q></td>
  108. </tr>
  109. <tr>
  110. <td>knownhosts</td>
  111. <td>This sets the known hosts file to use to validate the identity of the remote host. This
  112. must be a SSH2 format file. SSH1 format is not supported.</td>
  113. <td>No; defaults to <samp>${user.home}/.ssh/known_hosts</samp></td>
  114. </tr>
  115. <tr>
  116. <td>failonerror</td>
  117. <td>Whether to halt the build if the transfer fails.</td>
  118. <td>No; defaults to <q>true</q></td>
  119. </tr>
  120. <tr>
  121. <td>password</td>
  122. <td>The password.</td>
  123. <td>Yes, unless you are using key based authentication or the password has been given in the
  124. file or <var>todir</var> attribute</td>
  125. </tr>
  126. <tr>
  127. <td>keyfile</td>
  128. <td>Location of the file holding the private key.</td>
  129. <td>Yes, if you are using key based authentication</td>
  130. </tr>
  131. <tr>
  132. <td>passphrase</td>
  133. <td>Passphrase for your private key.</td>
  134. <td>No; defaults to an empty string</td>
  135. </tr>
  136. <tr>
  137. <td>verbose</td>
  138. <td>Determines whether SCP outputs verbosely to the user. Currently this means outputting
  139. dots/stars showing the progress of a file transfer. <em>since Ant 1.6.2</em></td>
  140. <td>No; defaults to <q>false</q></td>
  141. </tr>
  142. <tr>
  143. <td>sftp</td>
  144. <td>Determines whether SCP uses the sftp protocol. The sftp protocol is the file transfer
  145. protocol of SSH2. It is recommended that this be set to <q>true</q> if you are copying to/from
  146. a server that doesn't support scp1. <em>since Ant 1.7</em></td>
  147. <td>No; defaults to <q>false</q></td>
  148. </tr>
  149. <tr>
  150. <td>preserveLastModified</td>
  151. <td>Determines whether the last modification timestamp of downloaded files is preserved. It
  152. only works when transferring from a remote to a local system and probably doesn't work with a
  153. server that doesn't support SSH2. <em>since Ant 1.8.0</em></td>
  154. <td>No; defaults to <q>false</q></td>
  155. </tr>
  156. <tr>
  157. <td>filemode</td>
  158. <td>A 3 digit octal string, specify the user, group and other modes in the standard Unix
  159. fashion. Only applies to uploaded files. Note the actual permissions of the remote file will
  160. be governed by this setting and the <code>UMASK</code> on the remote server. <em>since Ant
  161. 1.9.5</em>.</td>
  162. <td>No; default is <q>644</q></td>
  163. </tr>
  164. <tr>
  165. <td>dirmode</td>
  166. <td>A 3 digit octal string, specify the user, group and other modes in the standard Unix
  167. fashion. Only applies to uploaded dirs. Note the actual permissions of the remote dir will
  168. be governed by this setting and the <code>UMASK</code> on the remote server. <em>since Ant
  169. 1.9.5</em>.</td>
  170. <td>No; default is <q>755</q></td>
  171. </tr>
  172. <tr>
  173. <td>serverAliveIntervalSeconds</td>
  174. <td>Sets a timeout interval in seconds after which if no data has been received from the server,
  175. the task will send a message through the encrypted channel to request a response from the
  176. server. <em>since Ant 1.9.7</em></td>
  177. <td>No, the default is <q>0</q>, indicating that these messages will not be sent to the
  178. server</td>
  179. </tr>
  180. <tr>
  181. <td>serverAliveCountMax</td>
  182. <td>The number of server alive messages which may be sent without receiving any messages back
  183. from the server. Only used if <var>serverAliveIntervalSeconds</var> is
  184. not <q>0</q>. <em>since Ant 1.9.7</em></td>
  185. <td>No; defaults to <q>3</q></td>
  186. </tr>
  187. <tr>
  188. <td>compressed</td>
  189. <td>Whether to enable compression during transfer. <em>since Ant 1.9.8</em></td>
  190. <td>No; defaults to <q>false</q></td>
  191. </tr>
  192. </table>
  193. <h3>Parameters specified as nested elements</h3>
  194. <h4>resource collections</h4>
  195. <p><a href="../Types/fileset.html">FileSet</a>s
  196. or other <a href="../Types/resources.html#collection">resource collections</a> are used to select groups
  197. of files to copy. To use a fileset or a resource collection, the <var>todir</var> attribute must be
  198. set.</p>
  199. <p>Prior to Ant 1.9.7 only <code>&lt;fileset&gt;</code> has been supported as a nested element.</p>
  200. <h3>Examples</h3>
  201. <p>Copy a single local file to a remote machine:</p>
  202. <pre>&lt;scp file=&quot;myfile.txt&quot; todir=&quot;user:password@somehost:/home/chuck&quot;/&gt;</pre>
  203. <p>Copy a single local file to a remote machine with separate <var>password</var>
  204. attribute:</p>
  205. <pre>&lt;scp file=&quot;myfile.txt&quot; todir=&quot;user@somehost:/home/chuck&quot; password=&quot;password&quot;/&gt;</pre>
  206. <p>Copy a single local file to a remote machine using key base authentication:</p>
  207. <pre>
  208. &lt;scp file=&quot;myfile.txt&quot;
  209. todir=&quot;user@somehost:/home/chuck&quot;
  210. keyfile=&quot;${user.home}/.ssh/id_dsa&quot;
  211. passphrase=&quot;my extremely secret passphrase&quot;/&gt;</pre>
  212. <p>Copy a single remote file to a local directory:</p>
  213. <pre>&lt;scp file=&quot;user:password@somehost:/home/chuck/myfile.txt&quot; todir=&quot;../some/other/dir&quot;/&gt;</pre>
  214. <p>Copy a remote directory to a local directory:</p>
  215. <pre>&lt;scp file=&quot;user:password@somehost:/home/chuck/*&quot; todir=&quot;/home/sara&quot;/&gt;</pre>
  216. <p>Copy a local directory to a remote directory:</p>
  217. <pre>
  218. &lt;scp todir=&quot;user:password@somehost:/home/chuck/&quot;&gt;
  219. &lt;fileset dir=&quot;src_dir&quot;/&gt;
  220. &lt;/scp&gt;</pre>
  221. <p>Copy a set of files to a directory:</p>
  222. <pre>
  223. &lt;scp todir=&quot;user:password@somehost:/home/chuck&quot;&gt;
  224. &lt;fileset dir=&quot;src_dir&quot;&gt;
  225. &lt;include name=&quot;**/*.java&quot;/&gt;
  226. &lt;/fileset&gt;
  227. &lt;/scp&gt;</pre>
  228. <p>Copy a set of files to a remote directory in reverse last-modified order:</p>
  229. <pre>
  230. &lt;scp todir=&quot;user:password@somehost:/home/chuck&quot;&gt;
  231. &lt;sort&gt;
  232. &lt;reverse&gt;
  233. &lt;date/&gt;
  234. &lt;/reverse&gt;
  235. &lt;fileset dir=&quot;src_dir&quot;&gt;
  236. &lt;include name=&quot;**/*&quot;/&gt;
  237. &lt;/fileset&gt;
  238. &lt;/sort&gt;
  239. &lt;/scp&gt;</pre>
  240. <p><strong>Security Note</strong>: Hardcoding passwords and/or usernames in <code>scp</code> task
  241. can be a serious security hole. Consider using variable substitution and include the password on
  242. the command line. For example:</p>
  243. <pre>&lt;scp todir=&quot;${username}:${password}@host:/dir&quot; ...&gt;</pre>
  244. <p>Invoking Ant with the following command line:</p>
  245. <pre class="input">ant -Dusername=me -Dpassword=mypassword target1 target2</pre>
  246. <p>is slightly better, but the username/password is exposed to all users on an Unix system (via
  247. the <kbd>ps</kbd> command). The best approach is to use the <code>&lt;input&gt;</code> task and/or
  248. retrieve the password from a (secured) <samp>.properties</samp> file.</p>
  249. <p><strong>Unix Note</strong>: File permissions are not retained when files are downloaded; they end
  250. up with the default <code>UMASK</code> permissions instead. This is caused by the lack of any means
  251. to query or set file permissions in the current Java runtimes. If you need a permission-preserving
  252. copy function, use <code>&lt;exec executable="scp" ... &gt;</code> instead.</p>
  253. </body>
  254. </html>