尝试将Unix命令的输出作为字符串返回在Java中…未获得预期结果。

huangapple go评论54阅读模式
英文:

Attempting to return the output of a Unix command as a string in Java... not getting expected results

问题

以下是您要翻译的内容:

我正在尝试在Java中创建一个小程序,它将允许我访问远程的Unix服务器,运行一些命令,然后在集成开发环境中显示这些命令的输出。

我一直在将代码片段组合在一起,并在学习的过程中尝试弄清楚它是如何工作的,这可能不是理想的方式!

目前,我能够从控制台运行命令,并且我能够在服务器上看到结果,但是我无法将输出发送回控制台。每当我尝试将它们作为字符串发送回来时,我得到的是类似于java.lang.UNIXProcess$ProcessPipeInputStream@9x57d1ad的东西,而不是我在服务器上看到的通常输出。我确定我肯定做错了什么明显的事情,但我是个初学者,尽管花了数小时搜索,但我还是没有找到解决方案。我真的很感激能得到一些帮助。谢谢!

在服务器上运行:

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(portNumber);
            Socket clientSocket = serverSocket.accept();
            PrintWriter out =
                    new PrintWriter(clientSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(clientSocket.getInputStream()));

            while ((inputLine = in.readLine()) != null) {

                out.println(inputLine);

                if (inputLine.equals("storage")) {
                    runStorage();
                    printResults(runStorage());
                }
            }
         } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Process runStorage() throws IOException {
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command("df", "-h");

        Process process = processBuilder.start();
        printResults(process);

        return process;
    }

    public static String printResults(Process process) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = "";
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        return line;
    }

客户端:

public class ClientInitiator {
    public static void main(String[] args) throws IOException, InterruptedException {
        String hostName = "myHostNameGoesHere";
        Socket echoSocket = new Socket(hostName, portNumber);
        PrintWriter out =
                new PrintWriter(echoSocket.getOutputStream(), true);
        BufferedReader in =
                new BufferedReader(
                        new InputStreamReader(echoSocket.getInputStream()));
        BufferedReader stdIn =
                new BufferedReader(
                        new InputStreamReader(System.in));

        {
            String userInput;

            while (!(userInput = stdIn.readLine()).equals("")) {
                out.println(userInput);
                if (userInput.equals("storage")) {
                    String serverResponse = in.readLine();
                    System.out.println(serverResponse);
                }
            }
        }
    }
}
英文:

I'm trying to create a little program in Java which will allow me to access a remote Unix server, run some commands and then display the output of those commands in the IDE.

I've been smashing together bits of code and trying to learn how it works as I go along, which probably isn't ideal!

Currently, I'm able to run the commands from the console and I'm able to see the results on the server, but I've been unable to send the output back to the console. Whenever I try to send them back as a string, I'm getting something along the lines of java.lang.UNIXProcess$ProcessPipeInputStream@9x57d1ad instead of the usual output that I'm seeing on the server. I'm sure there's something obvious I'm doing wrong, but I'm a beginner and despite spending hours searching I've not been able to find a solution yet. I'd really appreciate some help. Thanks!

RUNNING ON SERVER:

public static void main(String[] args) {
    try {
        ServerSocket serverSocket = new ServerSocket(portNumber);
        Socket clientSocket = serverSocket.accept();
        PrintWriter out =
                new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(clientSocket.getInputStream()));

        while ((inputLine = in.readLine()) != null) {

            out.println(inputLine);

            if (inputLine.equals("storage")) {
                runStorage();
                printResults(runStorage()); }

     catch (Exception e) {

        e.printStackTrace();

    }

public static Process runStorage() throws IOException {

    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command("df", "-h");

    Process process = processBuilder.start();
    printResults(process);

    return process;

}

public static String printResults(Process process) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line = "";
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
    return line;  }

CLIENT:

        public class ClientInitiator {

            public static void main(String[] args) throws IOException, InterruptedException {

                String hostName = "myHostNameGoesHere";
                Socket echoSocket = new Socket(hostName, portNumber);
                PrintWriter out =
                        new PrintWriter(echoSocket.getOutputStream(), true);
                BufferedReader in =
                        new BufferedReader(
                                new InputStreamReader(echoSocket.getInputStream()));
                BufferedReader stdIn =
                        new BufferedReader(
                                new InputStreamReader(System.in));


                {
                    String userInput;

                    while (!(userInput = stdIn.readLine()).equals("")) {
                        out.println(userInput);
                        if (userInput.equals("storage")) {
                            String serverResponse = in.readLine();
                            System.out.println(serverResponse);}

答案1

得分: 0

为了获取正在运行的进程的输出,请尝试以下代码。

InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
  System.out.println(line);
}
英文:

In order to get output of a running process, try the following code.

InputStream is = process.getInputStream();  
InputStreamReader isr = new InputStreamReader(is);  
BufferedReader br = new BufferedReader(isr);  
String line;  
while ((line = br.readLine()) != null) {  
  System.out.println(line);  
}  

huangapple
  • 本文由 发表于 2020年8月4日 21:50:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63248351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定