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

发布于 2024-10-07 13:42:19 字数 669 浏览 0 评论 0原文

我需要从 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);
}

I need to execute from Java a batch script, which does following

1) Once it is started it performs a lengthy (up to several seconds) task.

2) Thereafter, it displays a prompt "Password:".

3) Then, the user types in the password and presses the Enter key.

4) Then, the script completes its job.

I know how to launch the script from Java, I know how to read output of the batch script in Java, but I don't know how to wait for the password prompt to appear (how I get to know that the batch script is awaiting the password entry).

So, my question is: How to get to know when the batch script has printed the prompt?

At the moment, I have 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱&纷飞 2024-10-14 13:42:19

那应该可以完成这项工作:

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

That should do the job:

  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);
}
岁月蹉跎了容颜 2024-10-14 13:42:19

这似乎有效:

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

This one seems to work:

@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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文