Implemented IP Scan api
This commit is contained in:
parent
985cb8b1de
commit
b77631535e
1 changed files with 64 additions and 38 deletions
102
src/zutil/net/InetScanner.java
Normal file → Executable file
102
src/zutil/net/InetScanner.java
Normal file → Executable file
|
|
@ -4,69 +4,86 @@ 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Ziver on 2016-09-11.
|
* This class is a IPv4 scanner, it will scan a
|
||||||
|
* range of IPs to check if they are available.
|
||||||
|
* Note that this class uses the platform specific
|
||||||
|
* ping executable to check for availability.
|
||||||
*/
|
*/
|
||||||
public class InetScanner {
|
public class InetScanner {
|
||||||
private static final int TIMEOUT_MS = 50;
|
private static final int TIMEOUT_MS = 50;
|
||||||
|
|
||||||
|
private InetScanListener listener;
|
||||||
|
private boolean canceled;
|
||||||
|
|
||||||
public static void main(String[] args){
|
|
||||||
//scan();
|
public void setListener(InetScanListener listener){
|
||||||
scan2();
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void scan(){
|
/**
|
||||||
for (int i = 1; i < 255; i++) {
|
* Starts scanning a /24 ip range. This method will block until the scan is finished
|
||||||
String ip = "192.168.1."+i;
|
*
|
||||||
System.out.println(ip+": "+isReachableByPing(ip));
|
* @param ip the network ip address
|
||||||
}
|
*/
|
||||||
}
|
public synchronized void scan(InetAddress ip){
|
||||||
public static boolean isReachableByPing(String host) {
|
canceled = false;
|
||||||
|
MultiCommandExecutor exec = new MultiCommandExecutor();
|
||||||
|
String netAddr = ip.getHostAddress().substring(0, ip.getHostAddress().lastIndexOf('.')+1);
|
||||||
|
|
||||||
try{
|
try{
|
||||||
String[] output = OSAbstractionLayer.exec(getPlatformPingCmd(host));
|
for (int i = 1; i < 255 && !canceled; i++) {
|
||||||
if (output[2].contains("TTL=") || output[2].contains("ttl="))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
} catch( Exception e ) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static void scan2(){
|
|
||||||
try{
|
|
||||||
MultiCommandExecutor exec = new MultiCommandExecutor();
|
|
||||||
// execute the desired command (here: ls) n times
|
|
||||||
for (int i = 1; i < 255; i++) {
|
|
||||||
try {
|
try {
|
||||||
String ip = "192.168.1."+i;
|
String targetIp = netAddr+i;
|
||||||
exec.exec(getPlatformPingCmd(ip));
|
exec.exec(platformPingCmd(targetIp));
|
||||||
|
|
||||||
System.out.print(ip+": ");
|
|
||||||
boolean online = false;
|
boolean online = false;
|
||||||
for (String line; (line=exec.readLine()) != null;) {
|
for (String line; (line=exec.readLine()) != null;) {
|
||||||
if (line.contains("TTL=") || line.contains("ttl="))
|
if (platformPingCheck(line))
|
||||||
online = true;
|
online = true;
|
||||||
}
|
}
|
||||||
System.out.println(online);
|
if (online && listener != null)
|
||||||
}
|
listener.foundInetAddress(InetAddress.getByName(targetIp));
|
||||||
catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println(e);
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exec.close();
|
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
exec.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getPlatformPingCmd(String ip){
|
/**
|
||||||
|
* Cancels the ongoing ip scan
|
||||||
|
*/
|
||||||
|
public void cancel(){
|
||||||
|
canceled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will check if the given IP is reachable (Pingable)
|
||||||
|
*/
|
||||||
|
public static boolean isReachable(InetAddress ip){
|
||||||
|
String[] output = OSAbstractionLayer.exec(platformPingCmd(ip.getHostAddress()));
|
||||||
|
|
||||||
|
boolean online = false;
|
||||||
|
for (String line : output) {
|
||||||
|
if (platformPingCheck(line))
|
||||||
|
online = true;
|
||||||
|
}
|
||||||
|
return online;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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_MS +" " + ip;
|
||||||
|
|
@ -77,4 +94,13 @@ public class InetScanner {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private static boolean platformPingCheck(String line){
|
||||||
|
return line.contains("TTL=") || line.contains("ttl=");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public interface InetScanListener {
|
||||||
|
void foundInetAddress(InetAddress ip);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue