-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecTask.java
More file actions
65 lines (56 loc) · 1.49 KB
/
Copy pathExecTask.java
File metadata and controls
65 lines (56 loc) · 1.49 KB
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
package uploadServerTool;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.Session;
public abstract class ExecTask extends ServerTask {
public ExecTask(Server server) {
super(server);
}
protected void _run(Session session) {
String[] commands = getCommand();
if (commands == null) {
throw new NullPointerException("commands is null");
}
for (String command : commands) {
if ((command == null) || (command.length() <= 0)) {
upMsgln(1, "exec: " + command + " is empty.");
} else {
exec(session, command);
}
}
}
protected void exec(Session session, String command) {
try {
upMsgln(0, "exec: " + command + " ");
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
InputStream in = channel.getInputStream();
channel.connect();
upMsgln(0, "{");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
for (;;) {
String line = reader.readLine();
if (line == null) {
break;
}
upMsgln(0, line);
}
} catch (Exception e) {
e.printStackTrace();
in.close();
upMsg(0, "}");
channel.disconnect();
upMsgFinish();
}
return;
} catch (Exception e) {
e.printStackTrace();
upMsgFailure();
upMsgln(1, e.getMessage());
}
}
protected abstract String[] getCommand();
}