Java 執行遠程主機shell命令代碼


pom文件:

<dependency>
            <groupId>org.jvnet.hudson</groupId>
            <artifactId>ganymed-ssh2</artifactId>
            <version>build210-hudson-1</version>
</dependency>

 

Java代碼:

package com.gosun.utils;


import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 *
 *  storm 遠程調用腳本..
 * @author chenwen
 *
 */
public class ShellRPC {

    private static Connection conn;
    /** 遠程機器IP */
    private static String ip;
    /** 用戶名 */
    private static String userName;
    /** 密碼 */
    private static String password;
    private static String charset = Charset.defaultCharset().toString();

    private static final int TIME_OUT = 1000 * 5 * 60;

    
    /**
     * 登錄
     * @return
     * @throws IOException
     */
    private static boolean getConnection(String username, String password) throws IOException {
        ip = ip;
        userName = userName;
        password = password;

        conn = new Connection(ip);
        conn.connect();
        return conn.authenticateWithPassword(userName, password);
    }

    /**
     * 執行腳本
     *
     * @param cmds
     * @return
     * @throws Exception
     */
    public static int exec(String cmds) throws Exception {
        InputStream stdOut = null;
        InputStream stdErr = null;
        String outStr = "";
        String outErr = "";
        int ret = -1;
        try {
            if (getConnection()) {
                // Open a new {@link Session} on this connection
                Session session = conn.openSession();
                // Execute a command on the remote machine.
                session.execCommand(cmds);

                stdOut = new StreamGobbler(session.getStdout());
                outStr = processStream(stdOut, charset);
                System.out.println("outStr" + outStr);
                stdErr = new StreamGobbler(session.getStderr());
                outErr = processStream(stdErr, charset);
                System.out.println("outErr" + outErr);
                session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);

                ret = session.getExitStatus();
            } else {
                throw new Exception("登錄遠程機器失敗" + ip); // 自定義異常類 實現略
            }
        } finally {
            if (conn != null) {
                conn.close();
            }
            stdOut.close();
            stdErr.close();
        }
        return ret;
    }

    /**
     * @param in
     * @param charset
     * @return
     * @throws Exception
     */
    private static String processStream(InputStream in, String charset) throws Exception {
        byte[] buf = new byte[1024];
        StringBuilder sb = new StringBuilder();
        while (in.read(buf) != -1) {
            sb.append(new String(buf, charset));
        }
        return sb.toString();
    }

}

 


免責聲明!

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



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