CommandExecute.java
3.9 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
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
122
123
124
125
126
127
128
129
130
package com.sitech.ismp.check.mbean;
import java.util.Date;
import com.sitech.ismp.check.util.RemoteController;
import com.sitech.ismp.check.util.RemoteControllerFactory;
import com.sitech.ismp.messageObject.cc.CommandParameters;
import com.sitech.ismp.messageObject.cc.CommandResults;
import com.sitech.util.upload.RomoteController;
public class CommandExecute implements CommandExecutorMBean {
public static String Exec_Flag_Success = "1";
public static String Exec_Flag_Failure = "-1";
RomoteController rc = null;
String _taskid = "";
int _type = 0;
String _content = "";
String _hostip = "";
String _hostusername = "";
String _hostpassword = "";
String _hostloginmode = "";
Date _begintime = null;
Date _endtime = null;
String _execresult = "";
String _execlog = "";
String _execflag = Exec_Flag_Failure;
String _failurereasion = "";
boolean isConnect = false;
public CommandResults executeCommands(CommandParameters cps) {
_execlog = "指令执行器准备开始执行命令行\n";
CommandResults crs = new CommandResults();
String res = init(cps);
if (res.trim().toUpperCase().equals("SUCCESS")) {
try {
_begintime = new Date();
_execresult = rc.getResultAsStr(_content);
_endtime = new Date();
_execlog += "执行命令行结束\n";
_execflag = Exec_Flag_Success;
} catch (Exception e) {
_endtime = new Date();
e.printStackTrace();
_execlog += "执行命令行结束\n";
_execflag = Exec_Flag_Failure;
_failurereasion = e.getMessage();
_execresult = e.getMessage();
} finally {
crs.setTASK_ID(_taskid);
crs.setTYPE(_type);
crs.setBEGIN_TIME(_begintime);
crs.setEND_TIME(_endtime);
crs.setEXEC_RESULT(_execresult);
crs.setEXEC_LOG(_execlog);
crs.setEXEC_FLAG(_execflag);
crs.setFAILURE_REASION(_failurereasion);
}
} else {
crs.setTASK_ID(_taskid);
crs.setTYPE(_type);
crs.setBEGIN_TIME(_begintime);
crs.setEND_TIME(_endtime);
crs.setEXEC_RESULT(_execresult);
crs.setEXEC_LOG(_execlog);
crs.setEXEC_FLAG(_execflag);
crs.setFAILURE_REASION(res);
}
return crs;
}
public String init(CommandParameters cps) {
_taskid = cps.getTASK_ID();
_type = cps.getTYPE();
// "cd /bnmsapp4/agent/bin;ksh startAll.sh"
// _content = cps.getCONTENT();
_content = "cd " + cps.getDB_URL() + ";sh " + cps.getCONTENT();
System.out.println("content is: " + _content);
_hostip = cps.getHOST_IP();
_hostusername = cps.getHOST_USERNAME();
_hostpassword = cps.getHOST_PASSWORD();
_hostloginmode = cps.getHOST_LOGIN_MODE();
if (String.valueOf(_type) != null && _type == 1) {
// SHELL指令方式
_execlog += "准备连接SHELL命令主机"+_hostip+"\n";
rc = RemoteControllerFactory.createRemoteController(_hostip, _hostloginmode, _hostusername, _hostpassword);
} else if (String.valueOf(_type) != null && (_type == 2 || _type == 3)) {
// SQL/存储过程指令方式,
}
try {
rc.initial();
isConnect = true;
_execlog += "连接SHELL命令主机"+_hostip+"成功\n";
} catch (Exception e) {
isConnect = false;
_execlog += "连接SHELL命令主机"+_hostip+"失败\n";
_failurereasion = e.getMessage();
return "连接主机" + _hostip + "失败";
}
return "SUCCESS";
}
/**
* @param args
*/
public static void main(String[] args) {
CommandExecute ce = new CommandExecute();
CommandParameters cps = new CommandParameters();
// cps.setCONTENT("stopAll.sh");cps.setDB_URL("/bnmsapp4/agent/bin")
cps.setTASK_ID("TaskID-mooker");
cps.setTYPE(1);
// cps.setCONTENT("ls -l");
// cps.setCONTENT("startAll.sh");
cps.setCONTENT("stopAll.sh");
cps.setDB_URL("/bnmsapp4/agent/bin");
cps.setHOST_IP("172.21.0.117");
cps.setHOST_USERNAME("bnmsapp4");
cps.setHOST_PASSWORD("bnmsapp4");
cps.setHOST_LOGIN_MODE("SSH");
CommandResults cr = ce.executeCommands(cps);
System.out.println(cr.getEXEC_LOG());
System.out.println("执行结果:" + cr.getEXEC_RESULT());
}
}