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();
        }
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM