Java: defining a custom prompt when running a script package from Java

I need to execute a script package from Java that executes

1) After starting it performs a long (up to several seconds) task.

2) Then the prompt "Password:" will be displayed.

3) Then the user enters the password and presses the Enter key.

4) Then the script exits.

I know how to run a script from Java, I know how to read the output of a script package in Java, but I do not know how to wait for the password hint to appear (how can I get to what the script package is waiting for the password to be entered).

So my question is: how do I know when a script package printed an invitation?

At the moment, I have the following code:

final Runtime runtime = Runtime.getRuntime();
final String command = ... ;

final Process proc = runtime.exec(command, null, this.parentDirectory);

final BufferedReader input = new BufferedReader(new InputStreamReader(
  proc.getInputStream()));

String line = null;

while ((line = input.readLine()) != null) {
 LOGGER.debug("proc: " + line);
}
+5
source share
2 answers

:

  public static void main(final String... args) throws IOException, InterruptedException {
    final Runtime runtime = Runtime.getRuntime();
    final String command = "..."; // cmd.exe

    final Process proc = runtime.exec(command, null, new File("."));

    final BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));

    StringBuilder sb = new StringBuilder();
    char[] cbuf = new char[100];
    while (input.read(cbuf) != -1) {
        sb.append(cbuf);
        if (sb.toString().contains("Password:")) {
            break;
        }
        Thread.sleep(1000);
    }
    System.out.println(sb);
}
+3

, :

@Override
public void run() throws IOException, InterruptedException {
    final Runtime runtime = Runtime.getRuntime();
    final String command = ...;

    final Process proc = runtime.exec(command, null, this.parentDirectory);

    final BufferedReader input = new BufferedReader(new InputStreamReader(
            proc.getInputStream()));

    String batchFileOutput = "";

    while (input.ready()) {
        char character = (char) input.read();
        batchFileOutput = batchFileOutput + character;
    }

    // Batch script has printed the banner
    // Wait for the password prompt
    while (!input.ready()) {
        Thread.sleep(1000);
    }

    // The password prompt isn't terminated by a newline - that why we can't use readLine.
    // Instead, we need to read the stuff character by character.
    batchFileOutput = "";

    while (input.ready() && (!batchFileOutput.endsWith("Password: "))) {
        char character = (char) input.read();
        batchFileOutput = batchFileOutput + character;
    }

    // When we are here, the prompt has been printed
    // It time to enter the password

    if (batchFileOutput.endsWith("Password: ")) {
        final BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(proc.getOutputStream()));

        writer.write(this.password);

        // Simulate pressing of the Enter key
        writer.newLine();

        // Flush the stream, otherwise it doesn't work
        writer.flush();
    }

    // Now print out the output of the batch script AFTER we have provided it with a password
    String line;

    while ((line = input.readLine()) != null) {
        LOGGER.debug("proc: " + line);
    }

    // Print out the stuff on stderr, if the batch script has written something into it
    final BufferedReader error = new BufferedReader(new InputStreamReader(
            proc.getErrorStream()));

    String errorLine = null;

    while ((errorLine = error.readLine()) != null) {
        LOGGER.debug("proc2: " + errorLine);
    }

    // Wait until the program has completed

    final int result = proc.waitFor();

    // Log the result
    LOGGER.debug("result: " + result);
}
+1

All Articles