使用 Java 完全重定向 CMD.exe 进程的输入/输出/错误流

发布于 2024-10-04 21:29:37 字数 1429 浏览 2 评论 0原文

我这个项目的目标是通过 Java 获得远程命令提示符的感觉。使用 TCP/IP 套接字,我的目标是在一台计算机上运行命令提示符进程,并将所有控制传输到另一侧。我立即偶然发现了 Runtime.getRuntime().exec() 和 Process 对象等。我已经解决了一半的问题。使用远程命令提示符,我可以运行单个命令,收集输出,然后将其发送回另一端。问题是,我似乎只能为每个命令提示符实例运行一个命令。这是行不通的(在我需要更改目录然后运行命令等的情况下)。我已经从这种情况中剥离了所有套接字/网络编程来向您展示(并为我创建一个更简单的测试环境)。

import java.io.*;


public class testingProgram {

 public static void main(String[] args) {

  Runtime rt = Runtime.getRuntime();
  StringBuilder inputMessage = new StringBuilder();
  String resultData;
  try {

   Process pr = rt.exec("cmd.exe /c net user");
   BufferedReader processInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
   BufferedReader errorProcessInput = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
   PrintWriter processOut = new PrintWriter(pr.getOutputStream());
   while( (resultData = processInput.readLine()) != null ) {
      inputMessage.append(resultData + "\n");
   }
   resultData = inputMessage.toString();
   System.out.print(resultData);
  } catch(IOException e) {

  } //catch(InterruptedException e) {

  //}
 }

}

我还有很多,但这就是我的问题所在。我可以使用来自套接字流的简单变量和消息自定义命令“net user”,所以这不是我的问题。我的问题是我需要创建一个持续的命令提示符实例,保留输入/输出的所有重定向。基本上,我希望能够在“net user”之后发送另一个命令。

我已经收集并重定向了输出流。我希望能够做类似的事情:

processOut.write("net user");

我希望能够使用它,让命令提示符运行命令,并保留输出(无论是来自 errorStream 还是 inputStream)。

我只是需要更多关于如何去做这件事的指导。

My aim with this project was to have a remote command prompt feel with Java. Using TCP/IP sockets, I was aiming to run a command prompt process on one computer, and virtually transmit all control to the other side. I immediately stumbled over Runtime.getRuntime().exec() and Process objects, etc. I've solved my problem about halfway. With my remote command prompt, I can run a single command, gather the output, and send it back to the other side. The problem is, I can only seem to run one command per command prompt instance. This won't do (with situations where I need to change directory and THEN run a command, etc). I've stripped all socket/networking programming from this situation to show you (and to create an easier testing environment for me).

import java.io.*;


public class testingProgram {

 public static void main(String[] args) {

  Runtime rt = Runtime.getRuntime();
  StringBuilder inputMessage = new StringBuilder();
  String resultData;
  try {

   Process pr = rt.exec("cmd.exe /c net user");
   BufferedReader processInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
   BufferedReader errorProcessInput = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
   PrintWriter processOut = new PrintWriter(pr.getOutputStream());
   while( (resultData = processInput.readLine()) != null ) {
      inputMessage.append(resultData + "\n");
   }
   resultData = inputMessage.toString();
   System.out.print(resultData);
  } catch(IOException e) {

  } //catch(InterruptedException e) {

  //}
 }

}

I have a lot more, but this is where my problem is. I can customize the command "net user" with a simple variable and message from the socketstream, so that's not my problem. My problem is that I need to create an ongoing command prompt instance, retaining all redirections of the input/output. Basically, I would like to be able to send another command AFTER "net user".

I have gathered and redirected the output stream. I want to be able to do something like:

processOut.write("net user");

I want to be able to use this, have the command prompt run the command, and retain the output (whether it be from the errorStream OR the inputStream).

I just need some more direction on how to go about doing this.

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

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

发布评论

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

评论(2

唐婉 2024-10-11 21:29:37

您应该研究多线程。您基本上想要的是一个持续运行并维护 rt 的线程。

像这样:

  String commandLine;
  while ((commandLine = System.in.readline()) != 'q') {
        Process pc = rt.exec(commandLine);
  }

有关多线程的进一步参考:
http://download.oracle.com/javase/tutorial/essential/concurrency /procthread.html

您的问题是您的程序在一次调用后终止。

干杯

You should look into multi threading. What you basically want is a thread which keeps running and maintaining the rt.

Like this:

  String commandLine;
  while ((commandLine = System.in.readline()) != 'q') {
        Process pc = rt.exec(commandLine);
  }

For further reference on multithreading:
http://download.oracle.com/javase/tutorial/essential/concurrency/procthread.html

You problem is that your program terminates after one call.

cheers

留一抹残留的笑 2024-10-11 21:29:37

您告诉命令解释器终止。删除cmd.exe后面的/C。

cmd /?
Starts a new instance of the Windows command interpreter

CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
    [[/S] [/C | /K] string]

/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains
...

You're telling the command interpreter to terminate. Remove the /C after cmd.exe.

cmd /?
Starts a new instance of the Windows command interpreter

CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
    [[/S] [/C | /K] string]

/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文