高手的存在,就是让服务10亿人的时候,你感觉只是为你一个人服务......

java调用shell脚本

目录

测试环境发布系统,通过java调用远程机器上的shell脚本,实现自动化发布工作。
在此记录一下java调用shell脚本的实现。

需要的jar包:ganymed-ssh2-build210-sources.jar 和apache的commons-io包commons-io-1.3.2.jar
pom.xml依赖:

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>

<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210</version>
</dependency>

最终实现的情况:
远程机器:192.168.xx.xx
用户名:xxxx
密码:xxxx
登陆后执行服务器上的脚本 /master/bin/a.sh
代码比较简单,如下 a.sh脚本:

1
2
3
echo "aaa"
echo "bbb"
exit 1

java实现:

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.focustech.fds.common.utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import org.apache.commons.io.IOUtils;
import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
/**
* RmtShellExecutor.java
*
* @author lit
*/

public class RmtShellExecutor {
/** */
/** */
private Connection conn;
/** */
/** 远程机器IP */
private String ip;
/** */
/** 用户名 */
private String usr;
/** */
/** 密码 */
private String psword;
private String charset = Charset.defaultCharset().toString();
private static final int TIME_OUT = 1000 * 5 * 60;
/** */
/**
* 构造函数
*
* @param ip
* @param usr
* @param ps
*/

public RmtShellExecutor() {
}
public RmtShellExecutor(String ip, String usr, String ps) {
this.ip = ip;
this.usr = usr;
psword = ps;
}
/** */
/**
* 登录
*
* @return
* @throws IOException
*/

private boolean login() throws IOException {
conn = new Connection(ip);
conn.connect();
return conn.authenticateWithPassword(usr, psword);
}
/** */
/**
* 执行脚本
*
* @param cmds
* @return
* @throws Exception
*/

public int exec(String cmds) throws Exception {
InputStream stdOut = null;
InputStream stdErr = null;
String outStr = "";
String outErr = "";
int ret = -1;
try {
if (login()) {
// 创建session
Session session = conn.openSession();
// 执行远程脚本.
session.execCommand(cmds);
stdOut = new StreamGobbler(session.getStdout());
outStr = processStream(stdOut, charset);
stdErr = new StreamGobbler(session.getStderr());
outErr = processStream(stdErr, charset);
session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
ret = session.getExitStatus();
}
else {
throw new Exception("登录远程机器失败" + ip);
}
}
finally {
if (conn != null) {
conn.close();
}
IOUtils.closeQuietly(stdOut);
IOUtils.closeQuietly(stdErr);
}
return ret;
}
/** */
/**
* @param in
* @param charset
* @return
* @throws IOException
* @throws UnsupportedEncodingException
*/

private 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();
}
// bash参数:bash /master/bin/a.sh
public static void execute(String bash) throws Exception {
RmtShellExecutor exe = new RmtShellExecutor("192.168.10.199", "admin", "focus1234");
System.out.println(exe.exec(bash));
}
}

~

结果如下:
aaa
bbb
1