[Reprinted] Use ant for SSH and SCP operations

Source: Internet
Author: User
Tags scp file

I. Introduction:
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
Http://www.jcraft.com/jsch/index.html download a dependency package jsch-0.1.24.jar (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.

Ii. Simple Example:
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]

<sshexec host="127.16.1.198" username="${server.username}" password="${server.pwd}" command="/home/pcer/apache-tomcat-7.0.22/bin/shutdown.sh" trust="true" />

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 todir="root:123456@192.168.122.180:/tmp" file="c:/cmd.txt" trust="true" />

2. Copy the remote file to the local device.

<scp file="root:123456@192.168.122.180:/tmp/cmd.txt" todir="D:/my-app"  trust="true"/>

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.