Java 从Netbeans执行python脚本并读取输出在一种情况下失败,但在另一种情况下不会失败
我目前正在听取这个堆栈溢出帖子的建议: (用于获取输出的链接) 这很好,我成功地将其用于以下代码:Java 从Netbeans执行python脚本并读取输出在一种情况下失败,但在另一种情况下不会失败,java,python,netbeans,Java,Python,Netbeans,我目前正在听取这个堆栈溢出帖子的建议: (用于获取输出的链接) 这很好,我成功地将其用于以下代码: String directPath; if (Files.exists(Paths.get("C:/Users/jiaqin/Documents/NetBeansProjects/NCHandler/nchandlerv2/NCPythonFiles/"))) { directPath = "C:/Users/jiaqin/Documents
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脚本的路径来运行它,然后使用扫描器(而不是像本文中那样的BufferedReader)读取输出。基于这种情况,我有一个更晚的方法,使用了完全相同的概念:
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("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) + "\"");
因此,我在Netbeans输出中查找该行,结果如下:
Info: Python exec command is:
python /home/lab/testNetconf/NCPythonFiles/net_back_end/verify_show_commands.py -ssh_ip 9.0.0.12 -username root -password lab -show_commands "show bgp scale"
然后我复制粘贴到终端并运行它,我得到了一个输出(输出中打印结果的实际类型是字典,因此括号):
有人有什么建议吗?这让我非常沮丧,因为我使用get_platform_和os.py遵循第一个案例的确切格式,但是使用verify_show_commands.py,它什么也看不到
作为旁注,如果这些信息有帮助,当我在终端中手动运行命令时,大约需要2-3秒才能完成。然而,在我的项目中运行它只需要不到一秒钟的时间就可以出错。所以
显然这是一件事。我只是将命令中的每个单词改为数组元素,在数组中输入“show bgp scale”,但不带引号。在那之后,它成功了
Info: Python exec command is:
python /home/lab/testNetconf/NCPythonFiles/net_back_end/verify_show_commands.py -ssh_ip 9.0.0.12 -username root -password lab -show_commands "show bgp scale"