简体   繁体   English

使用processbuilder实现终端命令

[英]Achieving Terminal Commands using processbuilder

I am trying to execute the command present as the solution in the following link: https://apple.stackexchange.com/questions/258020/why-does-find-certificates-have-some-missing I am executing the command using processbuilder in java but for some reason i am not able to get the value from stringbuffer eventhough the command runs perfectly in terminal. 我正在尝试执行以下链接中作为解决方案提供的命令: https : //apple.stackexchange.com/questions/258020/why-does-find-certificates-have-some-missing我正在使用processbuilder执行命令在Java中,但是由于某种原因,即使命令在终端中运行得很好,我也无法从stringbuffer中获取值。 This is may java command: 这可能是java命令:

    ArrayList<String> lcommands = new ArrayList<String>();
        ArrayList<ArrayList<String>> lcommandsets = new ArrayList<ArrayList<String>>();
        lcommands = new ArrayList<String>(); 
        //lcommands.add("security find-identity -p codesigning -v");
//        lcommands.add("security");
//        lcommands.add("find-identity");
//        lcommands.add("-p");
//        lcommands.add("codesigning");
//        lcommands.add("-v");
        lcommands.add("security find-certificate -a -p codesign ~/Library/Keychains/login.keychain \\\n| awk '/-----BEGIN CERTIFICATE-----/ { cert = \"\" } \\\n{ cert = cert $0 \"\\n\" } \\\n/-----END CERTIFICATE-----/ { \\\nopenssl = \"openssl x509 -text -enddate -noout\"; \\\nprint cert | openssl; \\\nclose(openssl) \\\n}'");
//        lcommands.add("find-certificate");
//        lcommands.add("-a");
//        lcommands.add("-p");
//        lcommands.add("codesign");
        //lcommands.add("~/Library/Keychains/login.keychain \\\n| awk '/-----BEGIN CERTIFICATE-----/ { cert = \"\" } \\\n{ cert = cert $0 \"\\n\" } \\\n/-----END CERTIFICATE-----/ { \\\nopenssl = \"openssl x509 -text -enddate -noout\"; \\");
//        lcommands.add("~/Library/Keychains/login.keychain \\");
//        lcommands.add("| awk '/-----BEGIN CERTIFICATE-----/ { cert = \"\" } \\");
//        lcommands.add("{ cert = cert $0 \"\\n\" } \\");
//        lcommands.add("/-----END CERTIFICATE-----/ { \\");
//        lcommands.add("openssl = \"openssl x509 -text -enddate -noout\"; \\");
//        lcommands.add("print cert | openssl; \\");
//        lcommands.add("close(openssl) \\");
//        lcommands.add("}'");
        System.out.println();
        lcommandsets.add(lcommands);
        for (int i = 0; i < lcommandsets.size(); i++) {
            Process process = null;
            try {
                ArrayList lruncommands = (ArrayList) lcommandsets.get(i);
                ProcessBuilder lprocessbuilder = new ProcessBuilder(lruncommands);
            //    lprocessbuilder.directory(new File("/Users/"));
            //     lprocessbuilder.directory(new File("/Users/Admin/Library/Keychains"));
                 lprocessbuilder.redirectErrorStream(true);
                process = lprocessbuilder.start();
                try (BufferedReader bri = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                    String line;
                    while ((line = bri.readLine()) != null) {
                        //System.out.println(line);

                        if (line.contains(":") && line.contains("(")) {
                            lcertname = line.substring(line.indexOf(":") + 1, line.indexOf("(")).trim();
                            lteamid = line.substring(line.indexOf("(") + 1, line.lastIndexOf(")")).trim();
                            String ltrim=line.trim().substring(line.indexOf(')')+1);
                            luuid=ltrim.substring(0,ltrim.indexOf(" "));
                            //System.out.println("");
                            ArrayList<String> lval=new ArrayList<String>();
                            lval.add(0, luuid);
                            lval.add(1, lcertname);
                            lkeys.put(lteamid,lval );
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

All the commented lines are the different combinations that i tried.If anyone knows how to split the command,it would be verymuch useful.Thanks in advance. 所有带注释的行都是我尝试过的不同组合。如果有人知道如何拆分命令,那将非常有用。在此先感谢您。

The problem seems to be the "pipe" inside your command. 问题似乎出在您命令中的“管道”。 As far as I know you have to start a shell (cmd on windows) and pass your command-string as a parameter. 据我所知,您必须启动一个shell(在Windows上为cmd)并将命令字符串作为参数传递。

The following code starts a "git bash" under windows and executes the command "dig www.kde.org | grep kde". 以下代码在Windows下启动“ git bash”并执行命令“ dig www.kde.org | grep kde”。

    public static void main(String[] args) throws IOException {
    ArrayList<String> lst = new ArrayList<String>();

    lst.add("c:\\Anwendungen\\git\\bin\\bash");
    lst.add("-c");
    lst.add("dig www.kde.org |grep kde");
    ProcessBuilder bld = new ProcessBuilder(lst);

    Process proc = bld.start();

    BufferedReader bfRdr = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    bfRdr.lines().forEach((String line) -> {
        System.out.println(line);
    });
}

As an alternative you can try using another approach that I have used in my project. 或者,您可以尝试使用项目中使用的另一种方法。

    public static void main(String args[]) throws IOException

{
    String line = null;
    InputStream in = null;
    String logFilePath = System.getProperty("user.dir") + "/logs/log1.log";
    String s1 = "grep -n 'Start' " + logFilePath + "  | tail -n 1 | cut -d : -f 1";
    String[] cmd = { "/bin/sh", "-c", s1 };
    try {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(cmd);
        in = proc.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        line = br.readLine();

        // operations to be performed on matched line
    } finally {
        if (in != null)
            in.close();
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM