I am listing ip and mac address all devices in lan (like network scanner)
I want use java programming languages.
If i use my ip address,result is true but if i use another ip address in lan,network variable is null.
(for example ; my ip address : 192.168.1.7 , another ip address : 192.168.1.8)
Here my code;
public static void checkHosts(String subnet) throws UnknownHostException, IOException{
int timeout=3000;
for (int i=1;i<255;i++){
String host=subnet + "." + i;
if (InetAddress.getByName(host).isReachable(timeout)){
System.out.println(host + " is reachable" + InetAddress.getByName(host));
NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getByName(host));
if(network!=null){
System.out.println(network.isUp());
byte[] mac = network.getHardwareAddress();
System.out.println(network.getDisplayName());
System.out.println(network.getName());
System.out.println(InetAddress.getByName(host).getHostName());
System.out.print("Current MAC address : ");
StringBuilder sb = new StringBuilder();
for (int j = 0; j < mac.length; j++) {
sb.append(String.format("%02X%s", mac[j], (j < mac.length - 1) ? "-" : ""));
}
System.out.println(sb.toString());
}
}
}
}