JAVA远程执行Shell脚本类

1、java远程执行shell脚本类

 1 package com.test.common.utility;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.UnsupportedEncodingException;
 6 import java.nio.charset.Charset;
 7 
 8 import org.apache.commons.io.IOUtils;
 9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 
12 import ch.ethz.ssh2.ChannelCondition;
13 import ch.ethz.ssh2.Connection;
14 import ch.ethz.ssh2.Session;
15 import ch.ethz.ssh2.StreamGobbler;
16 
17 public class RmtShellExecutor {
18     /** 远程机器IP */
19     private final String ip;
20     /** 用户名 */
21     private final String usr;
22     /** 密码 */
23     private final String psword;
24 
25    /**
26      * 构造函数
27      * 
28      * @param ip
29      * @param usr
30      * @param ps
31     */
32     public RmtShellExecutor(String ip, String usr, String ps) {
33         this.ip = ip;
34         this.usr = usr;
35         this.psword = ps;
36     }
37 
38     /**
39      * 执行脚本
40      * 
41      * @param cmds
42      * @return
43      * @throws Exception
44     */
45     public int exec(String cmds) throws Exception {
46         InputStream stdOut = null;
47         InputStream stdErr = null;
48         String outStr = "";
49         String outErr = "";
50         int ret = -1;
51         try {
52             if (login()) {
53                 // Open a new {@link Session} on this connection
54                 Session session = conn.openSession();
55                 // Execute a command on the remote machine.
56                 session.execCommand(cmds);
57                 stdOut = new StreamGobbler(session.getStdout());
58                 outStr = processStream(stdOut, charset);
59                 stdErr = new StreamGobbler(session.getStderr());
60                 outErr = processStream(stdErr, charset);
61                 session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
62                 System.out.println("outStr=" + outStr);
63                 System.out.println("outErr=" + outErr);
64                 ret = session.getExitStatus();
65             } else {
66                 logger.info("登录远程机器失败" + ip);
67                 throw new AppException("登录远程机器失败" + ip); // 自定义异常类 实现略
68             }
69 
70         } finally {
71             if (conn != null) {
72                 conn.close();
73             }
74             IOUtils.closeQuietly(stdOut);
75             IOUtils.closeQuietly(stdErr);
76 
77         }
78         return ret;
79 
80     }
81 
82     private String processStream(InputStream in, String charset) throws Exception {
83         byte[] buf = new byte[1024];
84         StringBuilder sb = new StringBuilder();
85         while (in.read(buf) != -1) {
86             sb.append(new String(buf, charset));
87         }
88         return sb.toString();
89 
90     }
91 }
View Code


2、调用测试

1         RmtShellExecutor exe = new RmtShellExecutor("10.190.1.123", "root", "root");
2         try {
3             int i = exe.exec("sh /dcdata/etl/runjobinfo/cscscs.sh");
4             System.out.println(i);
5         } catch (Exception e) {
6             e.printStackTrace();
7             logger.info("执行dsjob错误" + e.getMessage());
8         }

 更新中。。。

原文地址:https://www.cnblogs.com/Nadim/p/4711235.html