List of usage examples for org.apache.commons.exec DefaultExecutor setStreamHandler
public void setStreamHandler(final ExecuteStreamHandler streamHandler)
From source file:com.demandware.vulnapp.challenge.impl.CommandInjectionChallenge.java
private String getCommandOutput(String command) { String output = null;/* w w w . j a v a 2s .c om*/ 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:io.werval.maven.MavenDevShellSPI.java
@Override protected void doRebuild() throws DevShellRebuildException { System.out.println("Reload!"); DefaultExecutor executor = new DefaultExecutor(); ByteArrayOutputStream output = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(output)); try {/*ww w. ja v a 2 s . c o m*/ int exitValue = executor.execute(cmdLine); if (exitValue != 0) { throw new DevShellRebuildException( new MavenExecutionException("Maven exited with a non-zero status: " + exitValue, pom)); } } catch (Exception ex) { throw new DevShellRebuildException(ex, output.toString()); } }
From source file:fitnesse.maven.io.CommandShell.java
public String execute(File workingDir, String... commands) { CommandLine commandLine = CommandLine.parse(commands[0]); for (int i = 1; i < commands.length; i++) { commandLine.addArgument(commands[i]); }/* w w w.j a v a2 s. co m*/ DefaultExecutor executor = new DefaultExecutor(); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(baos)); executor.setWorkingDirectory(workingDir); executor.execute(commandLine); return new String(baos.toByteArray()); } catch (ExecuteException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:hoot.services.command.CommandRunnerImpl.java
@Override public CommandResult exec(String command) throws IOException { logger.debug("Executing the following command: {}", command); try (OutputStream stdout = new ByteArrayOutputStream(); OutputStream stderr = new ByteArrayOutputStream()) { CommandLine cmdLine = CommandLine.parse(command); ExecuteStreamHandler executeStreamHandler = new PumpStreamHandler(stdout, stderr); DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(executeStreamHandler); int exitValue; try {// w w w . jav a 2 s . c o m exitValue = executor.execute(cmdLine); } catch (Exception e) { exitValue = -1; logger.warn("Error executing: {}", cmdLine, e); } CommandResult commandResult = new CommandResult(cmdLine.toString(), exitValue, stdout.toString(), stderr.toString()); this.stdout = stdout.toString(); logger.debug("Finished executing: {}", commandResult); return commandResult; } }
From source file:hoot.services.command.CommandRunnerImpl.java
@Override public CommandResult exec(String[] command) throws IOException { logger.debug("Executing the following command: {}", Arrays.toString(command)); try (OutputStream stdout = new ByteArrayOutputStream(); OutputStream stderr = new ByteArrayOutputStream()) { CommandLine cmdLine = new CommandLine(command[0]); for (int i = 1; i < command.length; i++) { cmdLine.addArgument(command[i], false); }//from w w w . j a v a 2s . com ExecuteStreamHandler executeStreamHandler = new PumpStreamHandler(stdout, stderr); DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(executeStreamHandler); int exitValue; try { exitValue = executor.execute(cmdLine); } catch (Exception e) { exitValue = -1; logger.warn("Error executing: {}", cmdLine, e); } CommandResult commandResult = new CommandResult(cmdLine.toString(), exitValue, stdout.toString(), stderr.toString()); this.stdout = stdout.toString(); logger.debug("Finished executing: {}", commandResult); return commandResult; } }
From source file:com.vmware.bdd.service.impl.NodeLdapUserMgmtConfService.java
private void transferFile(String srcFilePath, String ip, String targetFilePath) { CommandLine cmdLine = new CommandLine("scp").addArgument(srcFilePath) .addArgument(ip + ":" + targetFilePath); DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(new ExecOutputLogger(LOGGER, false), //output logger new ExecOutputLogger(LOGGER, true)) //error logger );//from w w w . j a v a 2s . c o m executor.setWatchdog(new ExecuteWatchdog(1000l * 120l)); try { int exitVal = executor.execute(cmdLine); if (exitVal != 0) { throw new RuntimeException("CFG_LDAP_FAIL", null); } } catch (IOException e) { throw new RuntimeException("CFG_LDAP_FAIL", e); } }
From source file:com.sonar.scanner.api.it.tools.CommandExecutor.java
public int execute(String[] args, Map<String, String> env, @Nullable Path workingDir) throws IOException { if (!Files.isExecutable(file)) { Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>(); perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.OWNER_EXECUTE); Files.setPosixFilePermissions(file, perms); }/* w w w .jav a2s . com*/ watchdog = new ExecuteWatchdog(TIMEOUT); CommandLine cmd = new CommandLine(file.toFile()); cmd.addArguments(args, false); DefaultExecutor exec = new DefaultExecutor(); exec.setWatchdog(watchdog); exec.setStreamHandler(createStreamHandler()); exec.setExitValues(null); if (workingDir != null) { exec.setWorkingDirectory(workingDir.toFile()); } in.close(); LOG.info("Executing: {}", cmd.toString()); return exec.execute(cmd, env); }
From source file:com.vmware.bdd.usermgmt.job.CfgUserMgmtOnMgmtVMExecutor.java
private void execCommand(CommandLine cmdLine) { DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(new ExecOutputLogger(LOGGER, false), //output logger new ExecOutputLogger(LOGGER, true)) //error logger );//from w ww.j a v a 2s . c o m executor.setWatchdog(new ExecuteWatchdog(1000l * TIMEOUT)); try { int exitVal = executor.execute(cmdLine); if (exitVal != 0) { throw new UserMgmtExecException("CFG_LDAP_FAIL", null); } } catch (IOException e) { throw new UserMgmtExecException("CFG_LDAP_FAIL", e); } }
From source file:it.sonarlint.cli.tools.CommandExecutor.java
public int execute(String[] args, @Nullable Path workingDir, Map<String, String> addEnv) throws IOException { if (!Files.isExecutable(file)) { Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>(); perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.OWNER_EXECUTE); Files.setPosixFilePermissions(file, perms); }//from w w w . ja v a2 s . com ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT); CommandLine cmd = new CommandLine(file.toFile()); cmd.addArguments(args); DefaultExecutor exec = new DefaultExecutor(); exec.setWatchdog(watchdog); exec.setStreamHandler(createStreamHandler()); exec.setExitValues(null); if (workingDir != null) { exec.setWorkingDirectory(workingDir.toFile()); } in.close(); LOG.info("Executing: {}", cmd.toString()); Map<String, String> env = new HashMap<>(System.getenv()); env.putAll(addEnv); return exec.execute(cmd, env); }
From source file:its.tools.CommandExecutor.java
public void execute(String[] args, @Nullable Path workingDir) throws IOException { if (!Files.isExecutable(file)) { Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>(); perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.OWNER_EXECUTE); Files.setPosixFilePermissions(file, perms); }// w w w .ja v a2 s.co m watchdog = new ExecuteWatchdog(TIMEOUT); CommandLine cmd = new CommandLine(file.toFile()); cmd.addArguments(args); DefaultExecutor exec = new DefaultExecutor(); exec.setWatchdog(watchdog); exec.setStreamHandler(createStreamHandler()); exec.setExitValues(null); if (workingDir != null) { exec.setWorkingDirectory(workingDir.toFile()); } in.close(); LOG.info("Executing: {}", cmd.toString()); exec.execute(cmd, new ResultHander()); }