Find all IP addresses in local network

只愿长相守 提交于 2019-12-09 16:37:54

问题


I want to find all IP addresses of devices in the local network I'm currently connected to using Java code. The useful utility Advanced IP Scanner is able to find various IP addresses in my subnet of 192.168.178/24:

According to this answer, I built my code the following way:

import java.io.IOException;
import java.net.InetAddress;

public class IPScanner
{
    public static void checkHosts(String subnet) throws IOException
    {
        int timeout = 100;
        for (int i = 1; i < 255; i++)
        {
            String host = subnet + "." + i;
            if (InetAddress.getByName(host).isReachable(timeout))
            {
                System.out.println(host + " is reachable");
            }
        }
    }

    public static void main(String[] arguments) throws IOException
    {
        checkHosts("192.168.178");
    }
}

Unfortunately, this does not print out any results, meaning that no IP addresses are reachable. Why? There are devices in my local network like seen in the Advanced IP Scanner scan.


回答1:


Try to increase the timeout. I used about 5000ms, this helped me. In case you don't want to wait 5000ms * 254 = 21 minutes, try also this code with parallel pinging to the addresses:

public static void getNetworkIPs() {
    final byte[] ip;
    try {
        ip = InetAddress.getLocalHost().getAddress();
    } catch (Exception e) {
        return;     // exit method, otherwise "ip might not have been initialized"
    }

    for(int i=1;i<=254;i++) {
        final int j = i;  // i as non-final variable cannot be referenced from inner class
        new Thread(new Runnable() {   // new thread for parallel execution
            public void run() {
                try {
                    ip[3] = (byte)j;
                    InetAddress address = InetAddress.getByAddress(ip);
                    String output = address.toString().substring(1);
                    if (address.isReachable(5000)) {
                        System.out.println(output + " is on the network");
                    } else {
                        System.out.println("Not Reachable: "+output);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();     // dont forget to start the thread
    }
}

Worked perfectly for me.




回答2:


InetAddress.isReachable will use ICMP ECHO REQUEST (as when you do a ping) or request on port 7 (echo port): http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#isReachable%28int%29

Advance IP scanner perhaps use an other way to discover the hosts (like a request on radmin port or a request on http).

An host can be up but not answering to ICMP ECHO REQUEST.

have you try to ping one of the host from command line?




回答3:


Maybe try using InetAddress.getByAddress(host) instead of getByName, like this:

    InetAddress localhost = InetAddress.getLocalHost();
    byte[] ip = localhost.getAddress();

    for (int i = 1; i <= 254; i++)
    {
        try
        {
            ip[3] = (byte)i; 
            InetAddress address = InetAddress.getByAddress(ip);

            if (address.isReachable(100))
            {
                output = address.toString().substring(1);
                System.out.print(output + " is on the network");
            }
    }

I took this sample for autodetection code from here




回答4:


Java 8 stream solution

    IntStream.rangeClosed(1,254).mapToObj(num -> "192.168.0."+num).parallel()
            .filter((addr) -> {
                try {
                    return InetAddress.getByName(addr).isReachable(2000);
                } catch (IOException e) {
                    return false;
                }
            }
            ).forEach(System.out::println);


来源:https://stackoverflow.com/questions/32500182/find-all-ip-addresses-in-local-network

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!