Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Im writing a java program that will scan a range of IP addresses with namp. The code isn't complete and there are a few issues i know i need to work though, so keep that in mind.

What i cant seem to figure out right now is how to view the output of of my nmap scan.

Right now the program will loop though 192.168.0.0 - 192.168.255.255 (infinitely, until i fix it) as confirmed with the system.out.println that's commented out. After that i run namp on the addresses but don't get any output. Ideas?

Below is the chunk of code with the nmap scan, its the first case in my switch statement:

case 1:
    IP = "192.168.";
    for (int i = 0; i < oct[1].length; i++) {
        for (int p = 0; p < oct[2].length; p++) {
            String addr = IP + 1 + "." + p;
//                  System.out.println(addr);

            Runtime aRT = Runtime.getRuntime();
            Process aProc = aRT.exec("/usr/local/bin/nmap -sn -n" + addr);
            //
            // HOW TO I VIEW THE RESULTS OF NMAP
            //
        }
    }

    break;

And here is the entire program thus far. Everything is in one class right now, i'll break it up later.

public class Start {
public static void main(String args[]) throws IOException {


    String s1 = getInput("Please select and IP range to scan: \n" + "\n" + 
            "1) 192.168.\n" + 
            "2) 172.28.\n" + 
            "3) 10.\n" 
            + "\n" + ":: ");

    int i1 = Integer.parseInt(s1);

    String IP = "";
    int oct[][] = new int[256][256];

    switch (i1) {
    case 1:
        IP = "192.168.";
        for (int i = 0; i < oct[1].length; i++) {
            for (int p = 0; p < oct[2].length; p++) {
                String addr = IP + 1 + "." + p;
//                  System.out.println(addr);

                Runtime aRT = Runtime.getRuntime();
                Process aProc = aRT.exec("/usr/local/bin/nmap -sn -n" + addr);
                //
                // HOW TO I VIEW THE RESULTS OF NMAP
                //
            }
        }

        break;
    case 2:

        System.out.println("case2");
        break;
    case 3:

        System.out.println("case3");
        break;
    case 99:

        System.out.println("case99");
        break;
    default:
        System.out.println("default");
        break;
    }



}
private static String getInput(String prompt) {
    BufferedReader stdin = new BufferedReader(
            new InputStreamReader(System.in));

    System.out.print(prompt);
    System.out.flush();

    try {
        return stdin.readLine();
    } catch (Exception e) {
        return "Error: " + e.getMessage();
    }
}

}
share|improve this question
add comment

closed as off topic by Joe F, Jeff Vanzella, p.s.w.g, Yuushi, svick Jun 3 '13 at 9:51

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

up vote 1 down vote accepted

You should be able to do something like this:

InputStream is = aProc.getInputStream();
Scanner sc = new Scanner(is);
while (sc.hasNextLine()) {
    System.out.println(sc.nextLine());
}
sc.close();
share|improve this answer
 
Works like a charm, thanks buddy! –  Cody H Jun 2 '13 at 20:06
add comment

Use the InputStream of the Process to read any output of the Process. In fact you should always do this, as per the documentation of the Process class :

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.