views:

39

answers:

1

I am trying to run multiple Nmap commands one after another.

Ideally, each Nmap command will be created in its own command prompt window. The Nmap command will execute and finish. Then another command prompt will appear with the next Nmap command, execute, and so on and so forth.

Unfortunately, the the way the program currently runs, multiple command prompt windows pop up at the same time and execute simultaneously. I want the commands to execute only one at a time. I had thought that the waitFor() method would solve the problem, yet it hasn't. Am I missing something?

I have simplified this greatly from my actual program to solve the core issue. Any help would be appreciated.

try {
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "start", "nmap", targets, "-p 1-    65535", "-oN +output");
            Process p = pb.start();
            p.waitFor();
            System.out.println("p done");

            Process z = pb.start();
            z.waitFor();
            System.out.println("z done");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
A: 

This is because you are doing a "start". The process will start another one to handle the nmap command, and terminates without waiting for that child process to terminate.

Removing the "start" should make the windows show one after the other.

Maurice Perry
I have found that if I remove "start" from the ProcessBuilder args, then no window will display at all.
Nomen
Well you can get the output of the processes with getInputStream() (!!!) and/or getErrorStream, and do whatever you want with it: send it to a log file, show it in a swing component, whatever...
Maurice Perry
Is there any way to make the "initial" process created by the ProcessBuilder visible as a command prompt window?While it is easy to display the output with getInputStream, how about interacting with the window as you would a command prompt window?When Nmap is running, if the process is taking a while, I can press a key in the command prompt causing Nmap to give an estimated time til completion of the command.
Nomen