Java ProcessBuilder 进程等待输入

Ori*_*ion 3 java windows process

通过 ProcessBuilder (特别是“GetMac /s”)运行命令行命令时,如果它抛出错误或正常返回,我可以读取错误或它返回的 MAC 地址,但如果它提示用户输入(网络上的某些电脑)使用 getmac 时需要密码)该进程将挂起等待密码。

以下是该命令从命令行运行时执行的操作: 在此输入图像描述

这是我用于该过程的代码:

package testing;
import java.io.IOException;

class test1 {
    public static void main(String[] args){
        String hostName = "testpc";
        ProcessBuilder builder = new ProcessBuilder("getmac", "/s", hostName, "/nh");
        builder.inheritIO();
        try {
            Process proc = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要 mac 的原因是为了唤醒 LAN 程序,并希望在检测到任何新电脑的 mac 地址时自动获取它们,这样用户就不必手动输入它。因此,如果您知道获取远程电脑的 MAC 的更好方法,请务必告诉我,我将使用它。

我意识到 java 可能不是最好的语言,但它是我目前唯一知道的语言,这只是我在工作中休息时的一个有趣的小项目。

**编辑:如果它需要密码,我只想忽略该电脑并终止进程并继续到下一台电脑

Hov*_*els 5

您需要处理与 Process 关联的所有 Stream,包括 InputStream、ErrorStream 和 OutputStream。您在命令行上看到的文本将通过 InputStream 传入,然后您将希望通过 OutputStream 传递请求的信息。

您需要在各自的线程中读取 InputStream 和 ErrorStream。如果它们传递文本,我经常将它们包装在 Scanner 对象中,并且我经常将 OutputStream 包装在 BufferedOutputStream 中,然后将其包装在 PrintStream 对象中。


例如,

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Scanner;

class Test1 {
   private static PrintStream out;

   public static void main(String[] args) {
      String hostName = "testpc";
      String[] commands = {"getmac", "/s", hostName,
            "/nh"};
      ProcessBuilder builder = new ProcessBuilder(commands);

      // builder.inheritIO(); // I avoid this. It was messing me up.

      try {
         Process proc = builder.start();
         InputStream errStream = proc.getErrorStream();
         InputStream inStream = proc.getInputStream();
         OutputStream outStream = proc.getOutputStream();

         new Thread(new StreamGobbler("in", out, inStream)).start();
         new Thread(new StreamGobbler("err", out, errStream)).start();

         out = new PrintStream(new BufferedOutputStream(outStream));
         int errorCode = proc.waitFor();
         System.out.println("error code: " + errorCode);
      } catch (IOException e) {
         e.printStackTrace();
      } catch (InterruptedException e) {
         e.printStackTrace();
      } finally {
         if (out != null) {
            out.close();
         }
      }
   }
}

class StreamGobbler implements Runnable {
   private PrintStream out;
   private Scanner inScanner;
   private String name;

   public StreamGobbler(String name, PrintStream out, InputStream inStream) {
      this.name = name;
      this.out = out;
      inScanner = new Scanner(new BufferedInputStream(inStream));
   }

   @Override
   public void run() {
      while (inScanner.hasNextLine()) {
         String line = inScanner.nextLine();

         // do something with the line!
         // check if requesting password

         System.out.printf("%s: %s%n", name, line);
      }      
   }
}
Run Code Online (Sandbox Code Playgroud)