CommandExecute.java 3.9 KB
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());
	}

}