Java 实现 SCP 携带密码拷贝文件
package com.miracle.luna.scp;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.scp.client.ScpClient;
import org.apache.sshd.scp.client.ScpClientCreator;
/**
* @author Miracle Luna
* @date 2021/7/19
*/
public class ScpUtil {
// private static String host = "192.168.67.48";
private static String host = "192.168.67.39";
// private static String host = "192.168.71.29";
private static String username = "root";
private static String password = "password";
private static Integer port = 22;
// private static String local = "F:\\miracle\\*";
private static String local = "/home/miracle/*";
private static String remote = "/home/luna/";
// private static String remote = "F:\\luna\\";
public static void scpFile(String local, String remote) throws Exception {
long startTime = System.currentTimeMillis();
// 创建 SSH客户端
SshClient client = SshClient.setUpDefaultClient();
// 启动 SSH客户端
client.start();
// 通过主机IP、端口和用户名,连接主机,获取Session
ClientSession session = client.connect(username, host, port).verify().getSession();
// 给Session添加密码
session.addPasswordIdentity(password);
// 校验用户名和密码的有效性
boolean isSuccess = session.auth().verify().isSuccess();
// 认证成功
if (isSuccess) {
long middleTime = System.currentTimeMillis();
System.out.println("Connect host cost time: " + (middleTime - startTime) / 1000.0 + "s.");
ScpClientCreator creator = ScpClientCreator.instance();
// 创建 SCP 客户端
ScpClient scpClient = creator.createScpClient(session);
System.out.println("Scp beginning.");
// ScpClient.Option.Recursive:递归copy,可以将子文件夹和子文件遍历copy
scpClient.upload(local, remote, ScpClient.Option.Recursive);
System.out.println("Scp finished.");
// 释放 SCP客户端
if (scpClient != null) {
scpClient = null;
}
// 关闭 Session
if (session != null && session.isOpen()) {
session.close();
}
// 关闭 SSH客户端
if (client != null && client.isOpen()) {
client.stop();
client.close();
}
}
long endTime = System.currentTimeMillis();
System.out.println("Total Cost time: " + (endTime - startTime) / 1000.0 + "s.");
}
public static void main(String[] args) throws Exception {
scpFile(local, remote);
}
}
2、运行结果
Connect host cost time: 6.519s.
Scp beginning.
Scp finished.
Total Cost time: 9.569s.
<!-- https://mvnrepository.com/artifact/org.apache.sshd/sshd-common -->
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-common</artifactId>
<version>2.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.sshd/sshd-core -->
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>2.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.sshd/sshd-scp -->
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-scp</artifactId>
<version>2.7.0</version>
</dependency>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)