Use ant for SSH and SCP operations

Source: Internet
Author: User
Tags scp file

From: http://www.blogjava.net/Unmi/archive/2006/01/03/124033.html

 

Currently, when installing Linux, we usually consider that the security (by default) does not enable the telnet service, while the SSH service does. ant has long supported Telnet, however, we need to install telnet-server in Linux and enable the service.

Fortunately, since ant1.60, ssh and SCP operations are supported, to support SSH, SCP, SFTP, and other tasks long before ant, you must download the j2ssh j2ssh-ant.jar and j2ssh-core.jar (downloaded under j2ssh for the http://www.sourceforge.net ). Now you can use the sshexec and SCP tasks provided by ant, supported by $ ant_home/lib/ant-jsch.jar, but you also need to download a dependency package jsch-0.1.24.jar in http://www.jcraft.com/jsch/index.html (file names vary by version ), jsch is also a project under the http://www.sourceforge.net.

You need to copy the downloaded jsch-0.1.24 to $ ant_home/lib, if it is the ant environment under eclipse, you must add the jsch-0.1.24 in window-> preferences-> ant-> runtime-> classpath.

Jsch is a pure Java implementation of SSH2.
Jsch allows you to connect to the sshd server and use port ing, X11 ing, SCP file transmission, etc. You can integrate the functions provided by jsch into your own Java project, jsch authorization is in the BSD format.

The following is the simplest example of using jsch to complete sshexec and SCP tasks. For more details, see the ant user manual.

[Sshexec task]
<Br> <Target Name= "Sshexec">
<Sshexec Host= "192.168.122.180"Username= "Root"Password= "123456"
Trust= "True"Command= "CD/; ls"/>
</Target>

Note that the trust attribute above is generally set to true. If the default value is false, the host you connect to must exist in your knownhosts file, this file must also exist, otherwise COM. jcraft. jsch. jschexception: reject hostkey: 192.168.122.180 exception. When executing commands in Linux, you can use semicolons (;) to separate multiple commands, which will be executed in sequence without writing multiple sshexec connections, only one command is executed for each connection.

After the task is executed, the output result is as follows:

Sshexec:
[Sshexec] connecting to 192.168.122.180: 22
[Sshexec] backup
[Sshexec] Bin
[Sshexec] boot
[Sshexec] Dev
[Sshexec] etc
...................

[SCP task]

1. Copy a single file to the remote server
<SCP File= "C:/cmd.txt"Todir= "Root: 123456@192.168.122.180:/tmp"Trust= "True"/>
Or
<SCP File= "C:/cmd.txt"Todir= "Root@192.168.122.180:/tmp"Password= "123456"Trust= "True"/>

2. Copy remote files locally
<SCP File= "Root: 123456@192.168.122.180:/tmp/cmd.txt"Todir= "D:/My-app"Trust= "True"/>

3. Copy the remote directory to the local device and perform recursive operations.
<SCP File= "Root: 123456@192.168.122.180:/tmp /*"Todir= "D:/My-app"Trust= "True"/>

4. Copy the content in the local directory to the remote end in recursive form, but do not create the my-app directory on the server.
<SCP Todir= "Root: 123456@192.168.122.180:/tmp /"Trust= "True">
<Fileset Dir= "D:/My-app"/>
</SCP>

5. Copy a series of files to the remote end. A Corresponding Hierarchical directory is created without the my-app directory.
<SCP Todir= "Root: 123456@192.168.122.180:/tmp"Trust= "True">
<Fileset Dir= "D:/My-app">
<Include Name= "**/*. Java"/>
</Fileset>
</SCP>
Or
<SCP Todir= "Root: 123456@192.168.122.180:/tmp"Trust= "True">
<Fileset Dir= "D:/My-app"Excludes= "**/*. Java"/>
</SCP>

The output result of the last task is as follows (others are omitted ):

SCP:
[SCP] connecting to 192.168.122.180: 22
[SCP] sending: cmd.txt: 0
[SCP] File Transfer Time: 0.0 average rate :? B/S
[SCP] sending: Pom. xml: 852
[SCP] File Transfer Time: 0.0 average rate: ∞ B/S
[SCP] sending: application. Property: 142
[SCP] File Transfer Time: 0.0 average rate: ∞ B/S
[SCP] sending: application. properties: 45
[SCP] File Transfer Time: 0.0 average rate: ∞ B/S
[SCP] sending: Test. properties: 0
[SCP] File Transfer Time: 0.02 average rate: 0.0 B/S
[SCP] sending: application. Property: 153
[SCP] File Transfer Time: 0.0 average rate: ∞ B/S
[SCP] sending: application. properties: 45
[SCP] File Transfer Time: 0.0 average rate: ∞ B/S
[SCP] Done.

[Unmi post-Note] many of the preceding content is directly visualized here, such as changing the font color and size. In this way, we find that there are too many HTML junk code generated, in the future, I will paste the rich text in the external editor, such as Dreamweaver, and paste it here to make a local backup better, so as to avoid accidents. Of course, it is okay to write the simple content directly here.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.