Java:从Java运行批处理脚本时检测用户提示

时间:2010-12-15 13:22:35

标签: java batch-file

我需要从Java执行一个批处理脚本,它遵循

1)一旦启动它就会执行一个冗长的(最多几秒)任务。

2)此后,它会显示提示“密码:”。

3)然后,用户输入密码并按Enter键。

4)然后,脚本完成其工作。

我知道如何从Java启动脚本,我知道如何在Java中读取批处理脚本的输出,但我不知道如何等待密码提示出现(我如何知道批处理脚本正在等待密码输入。)

所以,我的问题是:如何知道批处理脚本何时打印了提示?

目前,我有以下代码:

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);
}

2 个答案:

答案 0 :(得分:3)

那应该做的工作:

  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);
}

答案 1 :(得分:2)

这个似乎有效:

@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's 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's 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);
}