我目前正在关注此Stack Overflow帖子中的建议:
Call and receive output from Python script in Java?(链接到http://www.devdaily.com/java/edu/pj/pj010016以获得输出)
那么,太好了,我成功地将其用于以下代码:
default: return 'Postgres'; break;
从本质上讲,我先确定其路径来运行python脚本,然后使用Scanner读取输出(而不是后面的BufferedReader)。基于这种情况,我有一个后来的方法使用了完全相同的概念:
String directPath;
if (Files.exists(Paths.get("C:/Users/jiaqin/Documents/NetBeansProjects/NCHandler/nchandlerv2/NCPythonFiles/"))) {
directPath = "C:/Users/jiaqin/Documents/NetBeansProjects/NCHandler/nchandlerv2/NCPythonFiles/";
}
else {
directPath = "/home/lab/testNetconf/NCPythonFiles/net_back_end/";
}
Process p = Runtime.getRuntime().exec("python " + directPath + "get_platform_and_OS.py -ssh_ip " + ui.ssh_ip
+ " -username " + ui.username + " -password " + ui.password);
Scanner threeInfo = new Scanner(p.getInputStream());
String hostName = "";
String IP_Address = ui.ssh_ip;
String OS = "";
String platform = "";
int iter = 0;
while (threeInfo.hasNextLine()) {
switch(iter) {
case 0:
hostName = threeInfo.nextLine();
break;
case 1:
OS = threeInfo.nextLine();
break;
case 2:
platform = threeInfo.nextLine();
break;
default:
break;
}
iter ++;
}
threeInfo.close();
Label hostNameLabel = new Label(hostName);
Label sshLabel = new Label("SSH IP Address: " + IP_Address);
Label OSLabel = new Label(OS);
Label platformLabel = new Label(platform);
如您所见,我再次运行python脚本并读取输出。当我尝试获取retErrors的子字符串时发生错误-因为retErrors是一个空字符串;是的,由于某种原因,这次没有读取输出。注意,上面使用的是BufferedReader。我最初使用的是Scanner,但当我失败时,我进行了更改,因为我认为这可能是出于天知道的原因,同步可能会影响情况。不幸的是,似乎仍然没有输出要读取。
据我所知,我只是在Netbeans输出中观察并搜索了这一行
public static String verifyShowCommands(String[] showCommands, MainUI ui) {
try {
String errorText = "";
String retErrors = "";
System.out.println("Python exec command is:\n" + "python /home/lab/testNetconf/NCPythonFiles/net_back_end/verify_show_commands.py -ssh_ip " + ui.ssh_ip + " -username "
+ ui.username + " -password " + ui.password + " -show_commands \"" + String.join(", ", showCommands) + "\"");
Process p = Runtime.getRuntime().exec("python /home/lab/testNetconf/NCPythonFiles/net_back_end/verify_show_commands.py -ssh_ip " + ui.ssh_ip + " -username "
+ ui.username + " -password " + ui.password + " -show_commands \"" + String.join(", ", showCommands) + "\"");
BufferedReader obtainedInfo = new BufferedReader(new InputStreamReader(p.getInputStream()));
String read = null;
while ((read = obtainedInfo.readLine()) != null) {
System.out.println("New line found");
retErrors += read;
}
System.out.println("Obtained Info is:" + retErrors + " and that's it");
obtainedInfo.close();
for (int i = 0; i < showCommands.length; i ++) {
if (i < showCommands.length - 1) {
int thisSCStart = retErrors.indexOf(showCommands[i]);
int nextSCStart = retErrors.indexOf(showCommands[i+1]);
String outputSC = retErrors.substring(thisSCStart, nextSCStart);
if (outputSC.contains("'Valid': 'No'")) {
errorText += outputSC + "\n";
}
}
else {
String outputSC = retErrors.substring(retErrors.indexOf(showCommands[i]));
if (outputSC.contains("'Valid': 'No'")) {
errorText += outputSC + "\n";
}
}
}
return errorText;
}
catch (IOException e) {
return null;
}
}
然后,您可能会问一个明显的问题:如果命令无效,该怎么办?好吧,我也在顶部打印了整个exec命令:
System.out.println("Obtained Info is:" + retErrors + " and that's it");
因此,我在Netbeans输出中寻找该行,就在那儿:
System.out.println("Python exec command is:\n" + "python /home/lab/testNetconf/NCPythonFiles/net_back_end/verify_show_commands.py -ssh_ip " + ui.ssh_ip + " -username "
+ ui.username + " -password " + ui.password + " -show_commands \"" + String.join(", ", showCommands) + "\"");
然后我将该精确命令复制粘贴到终端中并运行它,然后得到一个输出(输出中打印结果的实际类型是字典,因此是方括号):
有人有什么建议吗?这让我非常沮丧,因为我使用get_platform_and_os.py遵循第一种情况的确切格式,但是随后使用verify_show_commands.py,它什么都不读。
作为旁注,如果此信息有用,那么当我在终端中手动运行命令时,大约需要2-3秒才能完成。但是,在我的项目中运行它只需要不到一秒钟的时间,就可以出错。
答案 0 :(得分:0)
所以...
Using Quotes within getRuntime().exec
显然这是一回事。我只是将命令中的每个单词都改为一个数组元素,并且在数组内部输入了“ show bgp scale”,但不带引号。在那之后,它起作用了。