0

I am trying to read the output of Psexec into Java using a BufferedReader on a Process InputStream for use on a network however it is only outputting the first line.

Runtime rt = Runtime.getRuntime();

    try {
        Process p = rt.exec("C:\\Users\\*****\\Desktop\\PS\\Psexec \\\\" + "******" + " -u ****** -p ****** cmd /c dir D:\\");

        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

        log.add("Computer: " + address);

        String s = null;
        while ((s = stdInput.readLine()) != null) {
            log.add(s);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

What would be the reason for this happening and how would this be fixed?

3 Answers3

2

The process is probably producing some of its output on stderr. Either read both the output and the error streams, in separate threads, or use the ProcessBuilder to create the Process, and merge the output streams before you do so, with redirectErrorStream().

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Tried that, did get the output from the remote machine :P – MadProgrammer Aug 19 '14 at 03:25
  • @MadProgrammer May I please have an example of that? – Funkydiddykong Aug 19 '14 at 04:08
  • @Funkydiddykong Sorry, that should be "didn't" - I tried paexec quick and got a better result, see my "extended comment"/answer for more details... – MadProgrammer Aug 19 '14 at 04:14
  • Sorry, I tried using ProcessBuildr, I threaded the reason of both the error and input streams and while I got the "psexec" output, I did not get the remote computer output. Both commands (as reported by psexec) competed with "0". I even tried redirecting the error/input through PIPE and INHERITED and out to a File without any difference. See the "extended comment"/answer – MadProgrammer Aug 19 '14 at 20:38
1

So, I spent some time playing around with this, using ProcessBuilder.

I tried redirecting the IO through the INHERITED and PIPE options, but could not get it to display the output of the remote command (the psexec content was fine)

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class Test1 {

    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder(
                        "C:\\Users\\shane\\Downloads\\PSTools\\PsExec.exe",
                        "\\\\builder",
                        "-u",
                        "xxx",
                        "-p",
                        "xxx",
                        "cmd", 
                        "/c", "dir", "c:\\"
        );
        try {
            Process p = pb.start();
            StreamConsumer.consume(p.getErrorStream());
            StreamConsumer.consume(p.getInputStream());
            System.out.println("Exited with :" + p.waitFor());
        } catch (IOException | InterruptedException exp) {
            exp.printStackTrace();
        }
    }

    public static class StreamConsumer implements Runnable {

        private InputStream is;

        public StreamConsumer(InputStream is) {
            this.is = is;
        }

        public static void consume(InputStream is) {
            StreamConsumer consumer = new StreamConsumer(is);
            new Thread(consumer).start();
        }

        @Override
        public void run() {
            try {
                int in = -1;
                while ((in = is.read()) != -1) {
                    System.out.print((char)in);
                }
            } catch (IOException exp) {
                exp.printStackTrace();
            }
        }

    }

}

I even tried redirecting the InputStreams to File without any success. It would seem that whatever mechanism psexec is using to stream the results from the remote machine don't seem to be picked up by Java.

You might try PAExec which did work, but didn't seem to wait to exit after the remote command exited...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • @Funkydiddykong Some executables can be a real pain - executing python is well known for a similar issue (no output)... – MadProgrammer Aug 19 '14 at 04:19
0

It could be the case that you started the process and didn't wait for it to finish before checking it's output. If this is the case, your main thread will exit your while loop because it reads null even though the subprocess is still executing. I would suggest using Process.waitFor() so that all of the output ends up in the stream before you begin polling it.

nullromo
  • 2,165
  • 2
  • 18
  • 39