ANT中的SSHEXEC和SCP任务用法

ANT中的SSHEXEC和SCP任务

本文通过简单示例介绍如何利用ant的sshexecscp执行ssh和scp命令的方法。

1 安装依赖jar包

JCRAFT上下载最新的jsch-*.jar。我下载的是jsch-0.1.53.jar
将该jar文件拷贝到$ANT_HOME/lib/下,即ant主目录下的lib/目录下。

2 SSHEXEC示例

代码:
exec.xml

<?xml version="1.0" encoding="UTF-8"?>
<project default="touch_somefile">
    <target name="touch_somefile">
        <sshexec host="localhost"
           username="${username}"
           password="${password}"
           command="touch /tmp/somefile2"
           trust="true"/>
    </target>
</project>

代码中的trust=”true”相当与ssh的 -o “StrictHostKeyChecking no“选项。
执行命令:

ant -Dusername=test -Dpassword=1234 -f exec.xml

3 SCP示例

核心代码:

<scp todir="${username}:${password}@somehost:/home/chuck">
  <fileset dir="src_dir">
    <include name="**/*.java"/>
  </fileset>
</scp>
<scp todir="${username}:${password}@somehost:/home/chuck">
  <fileset dir="src_dir" excludes="**/*.java"/>
</scp>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
scp是在Linux系统用于安全地在本地计算机和远程计算机之间复制文件或目录的命令。下面是scp的一些常见用法和场景: 1. 从本地复制文件到远程主机: ``` scp file.txt username@hostname:/path/to/destination ``` 这将将本地的file.txt文件复制到远程主机上的指定路径。需要替换`file.txt`为您要复制的本地文件路径,`username`为您的用户名,`hostname`为目标主机的IP地址或域名,`/path/to/destination`为目标主机上的目标路径。 2. 从远程主机复制文件到本地: ``` scp username@hostname:/path/to/file.txt /local/path/to/destination ``` 这将从远程主机上的指定路径复制file.txt文件到本地计算机的指定路径。需要替换`username`为远程主机的用户名,`hostname`为远程主机的IP地址或域名,`/path/to/file.txt`为远程主机上的文件路径,`/local/path/to/destination`为本地计算机上的目标路径。 3. 递归复制目录: ``` scp -r directory username@hostname:/path/to/destination ``` 这将递归地复制本地目录到远程主机上的指定路径。需要替换`directory`为您要复制的本地目录的路径。 4. 指定端口号: ``` scp -P port file.txt username@hostname:/path/to/destination ``` 这将通过指定的端口号连接到远程主机进行文件复制。需要替换`port`为远程主机的端口号。 5. 使用SSH密钥: ``` scp -i private_key.pem file.txt username@hostname:/path/to/destination ``` 这将使用指定的SSH密钥进行身份验证,并将file.txt文件复制到远程主机上的指定路径。需要替换`private_key.pem`为您的私钥文件路径。 这些只是scp的一部分用法和场景,更多详细的使用方法和选项可以通过man scp命令查看scp的手册页面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值