码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-11-22 17:13:14      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:end   condition   cep   man   cond   password   ssh2   exce   用户名   

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命令代码

标签:end   condition   cep   man   cond   password   ssh2   exce   用户名   

原文地址:https://www.cnblogs.com/unclecc/p/10001843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!