shell命令不是用java解释的

lmvvr0a8  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(379)

**结束。**此问题需要详细的调试信息。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

9天前关门了。
改进这个问题
我有这个街区;

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("bash -c \"mkdir .typo && mkdir .typo/lib && mkdir src/ && mkdir bin/ && ln -sFf .typo/lib lib && mkdir .typo/runtime && touch src/main.typo && echo \"@include !main\n\ndef main(str[255] args) {\n    std:out(\"Hello, world!\");\n\n    return 0;\n}\n\" >> src/main.typo\"");

try {
    process.waitFor();
} catch (InterruptedException interruptedException) {
    System.exit(130);
}

当我执行它时,什么也没发生。有时会发生,但大多数情况下不起作用。我也检查了文件系统,也没有什么不同。
(interruptedexception随 import java.lang.InterruptedException . )
我试过了,错误是;

.typo: -c: line 0: unexpected EOF while looking for matching `"'
.typo: -c: line 1: syntax error: unexpected end of file
6vl6ewon

6vl6ewon1#

您需要查看stderr输出,以便诊断命令正在执行的操作:
添加:

InputStream is = process.getErrorStream();
        System.out.println(IOUtils.toString(is));

创建流程之后。
ioutils来自:

<dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.8.0</version>
        </dependency>

电流输出为:

.typo: -c: line 0: unexpected EOF while looking for matching `"'
.typo: -c: line 1: syntax error: unexpected end of file
u2nhd7ah

u2nhd7ah2#

与owasp一致,我这样做是为了帮助使命令更具可读性,并检索它们的输出(一旦执行)。

public class SafeShellExecution {

    public String Execute(String[] command) {

        StringBuilder strAppend = new StringBuilder();

        try {
                String line;
                Process p = Runtime.getRuntime().exec(command);
                BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
                while ((line = in.readLine()) != null) {
                    strAppend.append(line);
                }
                in.close();
        } catch (IOException ex) {
            Logging.LogException(ex);
        }

        return strAppend.toString();
    }

}

然后清楚地定义命令:

public static final String[] GetIPAddress = {
        "/bin/sh",
        "-c",
        "ifconfig | grep -v '127.0.0.' | grep -i 'inet ' | awk {' print $2 '} | paste -sd ','"
    };

然后执行:

SafeShellExecution.Execute(GetIPAddress);

相关问题