I have a for loop that loops infinity when i don't breakout of it and only loops once when i do break out of it. I'm sure im missing something simple.
The if and else should be identical aside from the "String addr = " line. Right now both only loop once. Removing the "break;" sends them into an infinite loop.
public Discover(String enterIP) throws IOException {
IP = enterIP;
if (IP.length() < 4) {
for (int i = 0; i < oct.length; i++) {
String addr = IP + i + "." + i + "." + i;
Runtime aRT = Runtime.getRuntime();
Process aProc = aRT.exec("/usr/local/bin/nmap -sn -n " + addr);
InputStream is = aProc.getInputStream();
Scanner sc = new Scanner(is);
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
// if (sc.findInLine("Host is up") != null) {
// System.out.println(addr + "is up");
// }
}
sc.close();
break;
}
} else
for (int i = 0; i < oct.length; i++) {
String addr = IP + 1 + "." + i;
Runtime aRT = Runtime.getRuntime();
Process aProc = aRT.exec("/usr/local/bin/nmap -sn -n " + addr);
InputStream is = aProc.getInputStream();
Scanner sc = new Scanner(is);
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
// while(sc.findWithinHorizon("Host is up", 9999999) !=
// null){ // need to loop, stops after first find
// System.out.println(addr + " is up");
} sc.close();
break;
}
}
}