Java执行Shell和传输文件
特性:
- 多线程批量执行
- 多密码尝试
引入依赖:
<dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-ssh2</artifactId> <version>262</version> </dependency>
示例代码:
package com.example.demo; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.SCPClient; import ch.ethz.ssh2.SCPOutputStream; import ch.ethz.ssh2.Session; import org.apache.commons.io.IOUtils; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.concurrent.CountDownLatch; /** * SshFile * * @author hackyo * @date 2020/9/1 */ public class SshFile { private static final String[] HOSTS = { "10.72.30.207", "10.72.30.208", "10.72.30.209", "10.72.30.211", "10.72.30.212", "10.72.30.213" }; private static final String USERNAME = "root"; private static final String[] PASSWORDS = {"123", "456"}; private static final CountDownLatch LATCH = new CountDownLatch(HOSTS.length); public static void main(String[] args) { System.out.println("程序运行"); System.out.println(); //传输文件 File localFile = new File("C:\\Users\\13712\\Desktop\\CentOS-Base.repo"); String remoteDir = "/root/"; for (String host : HOSTS) { ScpThread scpThread = new ScpThread(host, localFile, remoteDir); scpThread.start(); } //执行命令 String exec = "echo '成功'"; for (String host : HOSTS) { ExecThread execThread = new ExecThread(host, exec); execThread.start(); } try { LATCH.await(); System.out.println("程序结束"); } catch (InterruptedException e) { System.out.println("程序错误:" + e.getMessage()); } } private static class ExecThread extends Thread { private final String host; private final String exec; private ExecThread(String host, String exec) { this.host = host; this.exec = exec; } @Override public void run() { StringBuilder re = new StringBuilder("开始连接-" + host + ":"); Connection conn = new Connection(host); try { conn.connect(); boolean isAuthenticated = false; for (String password : PASSWORDS) { isAuthenticated = conn.authenticateWithPassword(USERNAME, password); if (isAuthenticated) { break; } } if (isAuthenticated) { Session session = conn.openSession(); session.execCommand(exec); InputStream stdoutIn = session.getStdout(); re.append("执行输出:"); re.append(IOUtils.toString(stdoutIn)); re.append(";执行成功;"); stdoutIn.close(); session.close(); } else { re.append("无法登录;"); } } catch (Exception e) { re.append("执行失败:").append(e.getMessage()).append(";"); } conn.close(); System.out.println(re.toString()); System.out.println(); LATCH.countDown(); } } private static class ScpThread extends Thread { private final String host; private final File localFile; private final String remoteDir; private ScpThread(String host, File localFile, String remoteDir) { this.host = host; this.localFile = localFile; this.remoteDir = remoteDir; } @Override public void run() { StringBuilder re = new StringBuilder("开始连接-" + host + ":"); Connection conn = new Connection(host); try { conn.connect(); boolean isAuthenticated = false; for (String password : PASSWORDS) { isAuthenticated = conn.authenticateWithPassword(USERNAME, password); if (isAuthenticated) { break; } } if (isAuthenticated) { SCPClient scpClient = new SCPClient(conn); SCPOutputStream out = scpClient.put(localFile.getName(), localFile.length(), remoteDir, null); FileInputStream in = new FileInputStream(localFile); IOUtils.copy(in, out); in.close(); out.close(); re.append("传输成功;"); } else { re.append("无法登录;"); } } catch (Exception e) { re.append("传输失败:").append(e.getMessage()).append(";"); } conn.close(); System.out.println(re.toString()); System.out.println(); LATCH.countDown(); } } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战