admin
2022-03-29 fade37e2d7232a2818959f8c0f4eeca7304c3b3e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package org.yeshi.utils;
 
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
 
import java.io.*;
 
/**
 * 远程Linux系统命令执行
 */
public class LinuxRemoteCommandUtil {
 
    private static String DEFAULTCHART = "UTF-8";
 
    /**
     * 登录主机
     *
     * @return 登录成功返回true,否则返回false
     */
    public static Connection login(String ip,
                                   String userName,
                                   String userPwd) {
        boolean flg = false;
        Connection conn = null;
        try {
            conn = new Connection(ip);
            conn.connect();//连接
            flg = conn.authenticateWithPassword(userName, userPwd);//认证
            if (flg) {
                return conn;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return conn;
    }
 
    /**
     * 远程执行shll脚本或者命令
     *
     * @param cmd 即将执行的命令
     * @return 命令执行完后返回的结果值
     */
    public static String execute(Connection conn, String cmd) {
        String result = "";
        try {
            if (conn != null) {
                Session session = conn.openSession();//打开一个会话
                session.execCommand(cmd);//执行命令
                result = processStdout(session.getStdout(), DEFAULTCHART);
                //如果为得到标准输出为空,说明脚本执行出错了
                if (StringUtil.isNullOrEmpty(result)) {
                    result = processStdout(session.getStderr(), DEFAULTCHART);
                }
                conn.close();
                session.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
 
    /**
     * 解析脚本执行返回的结果集
     *
     * @param in      输入流对象
     * @param charset 编码
     * @return 以纯文本的格式返回
     */
    private static String processStdout(InputStream in, String charset) {
        InputStream stdout = new StreamGobbler(in);
        StringBuffer buffer = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line + "\n");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }
 
 
    /**
     *
     * @param ip
     * @param userName
     * @param pwd
     * @param cmd
     * @throws Exception
     */
    public static void execute(String ip, String userName, String pwd, String cmd) throws Exception {
        Connection connection = login(ip, userName, pwd);
        if (connection == null)
            throw new Exception("登录失败");
        execute(connection, cmd);
    }
 
}