I am currently heeding advice from this Stack Overflow post:
Call and receive output from Python script in Java? (which links to http://www.devdaily.com/java/edu/pj/pj010016 for getting the output)
So, that's great and all, and I used it for the following code successfully:
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);
Essentially, I run a python script by determining its path first, then I read the output using a Scanner (instead of a BufferedReader like in the post). Based on that case, I have a later method that uses the exact same concept:
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;
}
}
As you can see, I'm once again running a python script and reading the output. The error occurs when I attempt to get the substring of retErrors - because retErrors is an empty string; that's right, for some reason, this time the output is NOT getting read. Note that the above uses BufferedReader; I was originally using Scanner, and when that failed I changed over because I thought maybe for god-knows-what reason, synchronization might affect the situation. Unfortunately, it seemed there was STILL no output to read.
As for how I know, I simply observed in the Netbeans output and searched for the line
System.out.println("Obtained Info is:" + retErrors + " and that's it");
Then, you might ask the obvious question: what if the command is invalid? Well, I printed out the entire exec command at the top as well:
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) + "\"");
So, I looked for that line in the Netbeans output, and there it was:
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"
And then I copy-pasted that exact command into a terminal and ran it, and I got an output (the actual type of the printed result in the output is a dictionary, hence the brackets):
Anyone have some advice? This is extraordinarily frustrating to me because I'm following the exact format of the first case with get_platform_and_os.py but then with verify_show_commands.py, it just doesn't read anything.
As a side note, if this information helps, when I run the command manually in the terminal, it takes roughly 2-3 seconds to complete. However, running it in my project takes much less than a second before it errors out.