How to execute /bin/sh with commons-exec?

2019-07-10 03:46发布

This is what I'm doing:

import org.apache.commons.exec.*;
String cmd = "/bin/sh -c \"echo test\"";
new DefaultExecutor().execute(CommandLine.parse(cmd));

This is the output:

/bin/sh: echo test: command not found

What am I doing wrong?

5条回答
Deceive 欺骗
2楼-- · 2019-07-10 04:15

According to the FAQ "It is recommended to use CommandLine.addArgument() instead of CommandLine.parse()".

So try

CommandLine commandLine = new CommandLine("/bin/sh");
commandLine.addArgument("-c");
commandLine.addArgument("echo test", false); // false is important to prevent commons-exec from acting stupid
executor.execute(commandLine);
查看更多
狗以群分
3楼-- · 2019-07-10 04:22

The command doesn't work because commons-exec doing unnecesary quoting of all arguments, which have space or quote.

This unnecessary quoting of arguments is controlled by handleQuoting flag of each argument. If you create the CommandLine object using it's constructor and addArgument methods, you can set this flag to false.

Like this:

CommandLine commandLine = new CommandLine("/bin/sh");
commandLine.addArgument("-c");
commandLine.addArgument("echo test", false);

(The important part is false as second argument of addArgument method)

And it works! But... it is unconvinient to construct command line manually instead of having it defined in some config file.

CommandLine.parse always sets the handleQuoting flag to true! Who knows why...

I wrote this small helper method using reflection to fix "bad" CommandLine object, created using CommandLine.parse.

public static CommandLine fixCommandLine(CommandLine badCommandLine) {
    try {
        CommandLine fixedCommandLine = new CommandLine(badCommandLine.getExecutable());
        fixedCommandLine.setSubstitutionMap(badCommandLine.getSubstitutionMap());
        Vector<?> arguments = (Vector<?>) FieldUtils.readField(badCommandLine, "arguments", true);
        arguments.stream()
                .map(badArgument -> {
                    try {
                        return (String) FieldUtils.readField(badArgument, "value", true);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                })
                .forEach(goodArgument -> fixedCommandLine.addArgument(goodArgument, false));
        return fixedCommandLine;
    } catch (Exception e) {
        logger.error("Cannot fix bad command line", e);
        return badCommandLine;
    }
}

It just clones the given CommandLine, setting each argument's handleQuoting flag to false. The method FieldUtils.readField is from commons-lang3 library, but you can use plain reflection if you want.

It allows to parse command line and still successfully execute it.

String cmd = "/bin/sh -c 'echo test'";
new DefaultExecutor().execute(fixCommandLine(CommandLine.parse(cmd)));
查看更多
对你真心纯属浪费
4楼-- · 2019-07-10 04:24
import org.apache.commons.exec.*; 

CommandLine commandLine = new CommandLine("abc.sh");
commandLine.addArgument("param1"); 
final Executor exec = new DefaultExecutor().execute(commandLine);
查看更多
对你真心纯属浪费
5楼-- · 2019-07-10 04:26

I can reproduce your problem from a shell command line:

# /bin/sh -c '"echo test"'
# /bin/sh: echo test: command not found

I'd say you should try to not quote the command.

查看更多
淡お忘
6楼-- · 2019-07-10 04:28

This one works for me:-

    CommandLine command = new CommandLine("/bin/sh");
    command.addArguments(new String[] {
            "-c",
            "echo 'test'"
    },false);
    new DefaultExecutor().execute(command);
查看更多
登录 后发表回答