我正在尝试执行以下链接中的解决方案:https://apple.stackexchange.com/questions/258020/why-does-find-certificates-have-some-missing我在java中使用processbuilder执行命令但由于某种原因我无法通过命令从stringbuffer获取值在终端完美运行。 这可能是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();
}
所有注释的行都是我尝试过的不同组合。如果有人知道如何拆分命令,那将非常有用。谢谢提前。
答案 0 :(得分:0)
问题似乎是&#34;管道&#34;在你的命令里面。 据我所知,你必须启动一个shell(Windows上的cmd)并将命令字符串作为参数传递。
以下代码启动&#34; git bash&#34;在windows下执行命令&#34;挖掘www.kde.org | grep kde&#34;。
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);
});
}
答案 1 :(得分:0)
作为替代方案,您可以尝试使用我在项目中使用的其他方法。
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();
}
}
}