Some updates to ip scanner

This commit is contained in:
Ziver Koc 2016-10-03 17:48:01 +02:00
parent e15b633601
commit 93d17f4656

View file

@ -4,7 +4,6 @@ import zutil.osal.MultiCommandExecutor;
import zutil.osal.OSAbstractionLayer; import zutil.osal.OSAbstractionLayer;
import java.io.*; import java.io.*;
import java.net.Inet4Address;
import java.net.InetAddress; import java.net.InetAddress;
/** /**
@ -14,7 +13,7 @@ import java.net.InetAddress;
* ping executable to check for availability. * ping executable to check for availability.
*/ */
public class InetScanner { public class InetScanner {
private static final int TIMEOUT_MS = 50; private static final int TIMEOUT_SEC = 1; // 1 second
private InetScanListener listener; private InetScanListener listener;
private boolean canceled; private boolean canceled;
@ -39,7 +38,7 @@ public class InetScanner {
for (int i = 1; i < 255 && !canceled; i++) { for (int i = 1; i < 255 && !canceled; i++) {
try { try {
String targetIp = netAddr+i; String targetIp = netAddr+i;
boolean online = isReachable(InetAddress.getByName(targetIp)); boolean online = isReachable(targetIp, exec);
if (online && listener != null) if (online && listener != null)
listener.foundInetAddress(InetAddress.getByName(targetIp)); listener.foundInetAddress(InetAddress.getByName(targetIp));
} catch (IOException e) { } catch (IOException e) {
@ -65,8 +64,8 @@ public class InetScanner {
/** /**
* Will check if the given IP is reachable (Pingable) * Will check if the given IP is reachable (Pingable)
*/ */
public static boolean isReachable(InetAddress ip){ public static boolean isReachable(String host){
String[] output = OSAbstractionLayer.exec(platformPingCmd(ip.getHostAddress())); String[] output = OSAbstractionLayer.exec(platformPingCmd(host));
for (String line : output) { for (String line : output) {
if (platformPingCheck(line)) if (platformPingCheck(line))
@ -78,8 +77,8 @@ public class InetScanner {
* Will check if the given IP is reachable (Pingable). * Will check if the given IP is reachable (Pingable).
* This method is faster if multiple pings are needed as a MultiCommandExecutor can be provided. * This method is faster if multiple pings are needed as a MultiCommandExecutor can be provided.
*/ */
public static boolean isReachable(InetAddress ip, MultiCommandExecutor exec) throws IOException { public static boolean isReachable(String host, MultiCommandExecutor exec) throws IOException {
exec.exec(platformPingCmd(ip.getHostAddress())); exec.exec(platformPingCmd(host));
for (String line; (line=exec.readLine()) != null;) { for (String line; (line=exec.readLine()) != null;) {
if (platformPingCheck(line)) if (platformPingCheck(line))
@ -92,10 +91,10 @@ public class InetScanner {
private static String platformPingCmd(String ip){ private static String platformPingCmd(String ip){
switch (OSAbstractionLayer.getInstance().getOSType()){ switch (OSAbstractionLayer.getInstance().getOSType()){
case Windows: case Windows:
return "ping -n 1 -w "+ TIMEOUT_MS +" " + ip; return "ping -n 1 -w "+ TIMEOUT_SEC*1000 +" " + ip;
case Linux: case Linux:
case MacOS: case MacOS:
return "ping -c 1 -W "+ TIMEOUT_MS +" " + ip; return "ping -c 1 -W "+ TIMEOUT_SEC +" " + ip;
default: default:
return null; return null;
} }