List of usage examples for org.apache.commons.exec PumpStreamHandler PumpStreamHandler
public PumpStreamHandler(final OutputStream outAndErr)
PumpStreamHandler
. From source file:com.stratio.explorer.shell.ShellInterpreter.java
@Override public InterpreterResult interpret(String cmd) { logger.info("Run shell command '" + cmd + "'"); long start = System.currentTimeMillis(); CommandLine cmdLine = CommandLine.parse("bash"); cmdLine.addArgument("-c", false); cmdLine.addArgument(cmd, false);//from w w w. j a v a 2s . c o m DefaultExecutor executor = new DefaultExecutor(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(outputStream)); executor.setWatchdog(new ExecuteWatchdog(CMD_TIMEOUT)); try { int exitValue = executor.execute(cmdLine); return new InterpreterResult(InterpreterResult.Code.SUCCESS, outputStream.toString()); } catch (ExecuteException e) { logger.error("Can not run " + cmd, e); return new InterpreterResult(Code.ERROR, e.getMessage()); } catch (IOException e) { logger.error("Can not run " + cmd, e); return new InterpreterResult(Code.ERROR, e.getMessage()); } }
From source file:com.k42b3.sacmis.ExecutorAbstract.java
public void run() { try {/*from ww w .j av a2 s . c o m*/ // clear text this.textArea.setText(""); CommandLine commandLine = CommandLine.parse(this.getExecutable() + " " + this.cmd); ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); // create executor DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(new TextAreaOutputStream(textArea))); executor.setWatchdog(watchdog); executor.execute(commandLine); } catch (FoundNoExecutableException e) { JOptionPane.showMessageDialog(null, e.getMessage(), "Information", JOptionPane.ERROR_MESSAGE); } catch (IOException e) { logger.error(e.getMessage(), e); } }
From source file:com.demandware.vulnapp.challenge.impl.CommandInjectionChallenge.java
private String getCommandOutput(String command) { String output = null;/* w w w .j a va2 s . c o m*/ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { CommandLine cmd = new CommandLine("/bin/bash"); String[] args = new String[] { "-c", command }; cmd.addArguments(args, false); PumpStreamHandler psh = new PumpStreamHandler(outputStream); DefaultExecutor exec = new DefaultExecutor(); exec.setStreamHandler(psh); exec.execute(cmd); output = outputStream.toString(); } catch (ExecuteException e) { e.printStackTrace(); output = "Could not execute command"; } catch (IOException e) { e.printStackTrace(); } return output; }
From source file:com.github.mjeanroy.maven.plugins.node.commands.CommandExecutor.java
/** * Execute command line and return the result status. * * @param workingDirectory Working directory (i.e where the command line is executed). * @param command Command, containing executable path with arguments. * @param logger Logger to use to log command output. * @return Command result object./*from w w w . j av a 2 s . c o m*/ */ public CommandResult execute(File workingDirectory, Command command, Log logger) { CommandLine commandLine = new CommandLine(command.getExecutable()); for (String argument : command.getArguments()) { commandLine.addArgument(argument); } try { Executor executor = new DefaultExecutor(); executor.setWorkingDirectory(workingDirectory); executor.setExitValue(0); // Define custom output stream LogStreamHandler stream = new LogStreamHandler(logger); PumpStreamHandler handler = new PumpStreamHandler(stream); executor.setStreamHandler(handler); int status = executor.execute(commandLine); return new CommandResult(status); } catch (ExecuteException ex) { return new CommandResult(ex.getExitValue()); } catch (IOException ex) { throw new CommandException(ex); } }
From source file:com.github.genium_framework.appium.support.command.CommandManager.java
/** * Execute a command on the operating system using Apache Commons Exec. This * function runs asynchronously and dumps both stderr and stdout streams to * a temp file./*from w w w . j a v a 2 s. co m*/ * * @param commandLine The command to be executed. * @param outputStreamHandler An output stream to dump the process stderr * and stdout to it. */ public static void executeCommandUsingApacheExec(CommandLine commandLine, OutputStream outputStreamHandler) { try { DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStreamHandler); Executor process = new DefaultExecutor(); process.setExitValue(0); process.setStreamHandler(streamHandler); process.execute(commandLine, resultHandler); } catch (Exception ex) { LOGGER.log(Level.SEVERE, "An exception was thrown.", ex); } }
From source file:eu.creatingfuture.propeller.blocklyprop.propeller.PropellerLoad.java
protected boolean loadIntoRam(String executable, File ramFile, String comPort) { try {//from w ww . jav a 2s . co m Map map = new HashMap(); map.put("ramFile", ramFile); CommandLine cmdLine = new CommandLine(executable); cmdLine.addArgument("-r"); if (comPort != null) { cmdLine.addArgument("-p").addArgument(comPort); } cmdLine.addArgument("${ramFile}"); cmdLine.setSubstitutionMap(map); DefaultExecutor executor = new DefaultExecutor(); //executor.setExitValues(new int[]{451, 301}); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); executor.setStreamHandler(streamHandler); try { exitValue = executor.execute(cmdLine); } catch (ExecuteException ee) { exitValue = ee.getExitValue(); logger.log(Level.SEVERE, "Unexpected exit value: {0}", exitValue); success = false; return false; } finally { output = outputStream.toString(); } success = true; return true; } catch (IOException ioe) { logger.log(Level.SEVERE, null, ioe); success = false; return false; } }
From source file:com.blackducksoftware.tools.scmconnector.core.CommandLineExecutor.java
@Override public CommandResults executeCommandForOutput(Logger log, CommandLine command, File targetDir) throws Exception { DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0);/*from w w w . j a v a 2 s. c om*/ ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PumpStreamHandler psh = new PumpStreamHandler(outputStream); executor.setStreamHandler(psh); int exitStatus = 1; executor.setWorkingDirectory(targetDir); exitStatus = executor.execute(command); String outputString = outputStream.toString(); // This log msg would reveal password // log.info("Command: " + command.toString() + " executed in: " // + targetDir.toString() + "; output: " + outputString); return new CommandResults(exitStatus, outputString); }
From source file:at.treedb.util.Execute.java
public static ExecResult execute(String command, String[] param, Map<String, File> map) { DefaultExecutor executor = new DefaultExecutor(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); CommandLine cmdLine = null;/*from ww w. j a v a2s .c o m*/ int exitValue = 0; try { PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); cmdLine = new CommandLine(command); if (param != null) { for (String s : param) { s = s.trim(); if (s.isEmpty()) { continue; } cmdLine.addArgument(s); } } cmdLine.setSubstitutionMap(map); executor.setStreamHandler(streamHandler); exitValue = executor.execute(cmdLine); return new ExecResult(exitValue, outputStream.toString(), null); } catch (Exception e) { return new ExecResult(-1, outputStream.toString(), e); } }
From source file:io.manasobi.utils.CmdUtils.java
/** * (commandLine) ? ?? ? ? ?/* w w w . ja v a 2 s . com*/ * * @param commandLine * @param argument ? * @param timeout * * @return int ? */ public static int execute(CommandLine commandLine, String argument, long timeout) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(baos); DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(streamHandler); // ? if (StringUtils.isNotEmpty(argument)) { commandLine.addArguments(argument); } // if (timeout > 0) { ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog); } // ?? ? executor.setExitValue(SUCCESS_RETURN_CODE); try { DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); executor.execute(commandLine, resultHandler); resultHandler.waitFor(); return resultHandler.getExitValue(); } catch (Exception e) { throw new CmdUtilsException(e.getMessage()); } }
From source file:hu.bme.mit.trainbenchmark.benchmark.fourstore.driver.UnixUtils.java
public static void exec(final String command, final Map<String, String> environmentVariables, final OutputStream outputStream) throws IOException, ExecuteException { final Map<?, ?> executionEnvironment = EnvironmentUtils.getProcEnvironment(); for (final Entry<String, String> environmentVariable : environmentVariables.entrySet()) { final String keyAndValue = environmentVariable.getKey() + "=" + environmentVariable.getValue(); EnvironmentUtils.addVariableToEnvironment(executionEnvironment, keyAndValue); }/* w w w . j av a 2s . c om*/ final PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); final CommandLine commandLine = new CommandLine("/bin/bash"); commandLine.addArguments(new String[] { "-c", command }, false); final DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(streamHandler); executor.execute(commandLine, executionEnvironment); }