Java 执行远程主机shell命令代码

Posted 德艺双馨陈叔叔、

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
    }

}

 

以上是关于Java 执行远程主机shell命令代码的主要内容,如果未能解决你的问题,请参考以下文章

利用shell脚本执行ssh远程另一台主机执行命令并返回命令的结果集

Java 实现 ssh命令 登录主机执行shell命令

Java远程实现Linux文件内容读取(通过远程执行shell命令分析日志)

Java远程实现Linux文件内容读取(通过远程执行shell命令分析日志)

Java远程实现Linux文件内容读取(通过远程执行shell命令分析日志)

Java远程实现Linux文件内容读取(通过远程执行shell命令分析日志)