package com.pactera.common.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.cli.*;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.*;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.util.Collection;
* @author song
* @date created in 2018-01-19 18:49
*/
public class ExecuteShell {
public static JSONObject callCMD(String shellInfo) {
System.out.println("Cmd接收到的命令---"+shellInfo);
JSONObject json = new JSONObject();
String error = "";
int execute = 1;
String out="";
try {
CommandLine commandline = CommandLine.parse(shellInfo);
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValue(0);
ExecuteWatchdog watchdog = new ExecuteWatchdog(600 * 1000);
exec.setWatchdog(watchdog);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream);
exec.setStreamHandler(streamHandler);
execute = exec.execute(commandline);
out = outputStream.toString("gbk");
error = errorStream.toString("gbk");
System.out.println(out);
System.out.println(error);
} catch (Exception e) {
json.put("statusCode", 1);
json.put("msg", "Failed to call shell's command and the return status's is:1 "+error );
}
json.put("statusCode" , execute);
json.put("msg" , out);
return json;
}
* 创建commandline对象
*
* @param args
* @return
* @throws ParseException
*/
private static CommandLine getCommandLine(String args, Options mergeOptions) throws ParseException {
CommandLineParser parser = new DefaultParser();
HelpFormatter hf = new HelpFormatter();
hf.printHelp(args, "", mergeOptions, "");
return null;
}
* 合并options
*
* @param optionsArgs options项
* @return 合并后的options
*/
private static Options mergeOptions(Options... optionsArgs) {
Options allOptions = new Options();
for (Options options : optionsArgs) {
Collection<Option> options1 = options.getOptions();
for (Option option : options1) {
allOptions.addOption(option);
}
}
return allOptions;
}
@Test
public void test() throws ParseException {
String args = "sdo";
}
String argss[]={"-t 1000"};
//定义
Options options = new Options();
options.addOption("ipconfig", false, "list help");//false代表不强制有
options.addOption("ipconfig", true, "set time on system");
//解析
//1.3.1中已经弃用针对不同格式入参对应的解析器
//CommandLineParser parser = new PosixParser();
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args);
//查询交互
//你的程序应当写在这里,从这里启动
String formatstr = "ipconfig";
HelpFormatter hf = new HelpFormatter();
hf.printHelp(formatstr, "", options, "");
return;
}*/
public static void main(String[] args) {
try {
String command = " E:\\tool\\data-integration\\kitchen.bat /rep:kettle /dir:/ /user:admin /pass:admin /level:Basic /job:EDW_FENFA_JOB -param:\"DBTYPE=ORACLE\" -param:\"TABLE_NAME=omdata.S24_SEAT_HISTORY\" -param:\"EDW_TABLE_NAME=S24_SEAT_HISTORY\" -param:\"COLUMNS=T.ID,T.BUS_ID,T.LAST_REST,T.REST,T.LAST_UPDATETIME,T.UPDATE_TIME\" -param:\"USER_NAME=yyy\" -param:\"CONDITIONS=TO_DATE('20160310','YYYYMMDD')=T.ETL_DT\" -param:\"DATA_DT=20160310\"";
CommandLine commandline = CommandLine.parse(command);
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValue(0);
ExecuteWatchdog watchdog = new ExecuteWatchdog(60*1000);
exec.setWatchdog(watchdog);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream);
exec.setStreamHandler(streamHandler);
int execute = exec.execute(commandline);
String out = outputStream.toString("gbk");
String error = errorStream.toString("gbk");
System.out.println("out"+out);
System.out.println("error"+error);
System.out.println("exit: " + execute);
} catch (Exception e) {
return;
}
}
}