【问题标题】:Find all IP addresses in local network查找本地网络中的所有 IP 地址
【发布时间】:2015-12-06 15:22:16
【问题描述】:

我想使用 Java 代码查找当前连接到的本地网络中设备的所有 IP 地址。有用的实用程序Advanced IP Scanner 能够在我的subnet192.168.178/24 中找到各种IP 地址:

根据this 的回答,我按照以下方式构建了我的代码:

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");
    }
}

不幸的是,这不会打印出任何结果,这意味着无法访问任何 IP 地址。为什么?我的本地网络中有一些设备,就像在Advanced IP Scanner 扫描中看到的那样。

【问题讨论】:

标签: java networking network-scan


【解决方案1】:

尝试增加超时时间。我使用了大约 5000 毫秒,这对我有帮助。 如果您不想等待 5000 毫秒 * 254 = 21 分钟,请尝试使用此代码并行 ping 地址:

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
    }
}

非常适合我。

【讨论】:

  • 你从 1-254 而不是 0-255 有什么原因吗?
  • 在我的网络设置中,0 用作默认网关,255 用作广播,因此没有设备会连接到这两个地址。
【解决方案2】:

InetAddress.isReachable 将使用 ICMP ECHO REQUEST(当您执行 ping 时)或在端口 7(回显端口)上请求:http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#isReachable%28int%29

Advance IP 扫描器可能使用其他方式来发现主机(例如 radmin 端口上的请求或 http 上的请求)。

主机可以启动但不响应 ICMP ECHO REQUEST。

您是否尝试从命令行 ping 主机之一?

【讨论】:

  • 主机可以ping和回复
【解决方案3】:

也许尝试使用InetAddress.getByAddress(host) 而不是getByName,像这样:

    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");
            }
    }

我拿了这个样本作为自动检测代码from here

【讨论】:

  • 是你吗,VGMoose?干杯人 ;) 但是,您的代码和应用程​​序无法找到我的 Nintendo Wii 控制台,因此您的网络扫描无法正常工作,抱歉。文件传输有效,但我需要手动输入 Wii 的 IP 地址
【解决方案4】:

Java 8 流解决方案

    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);

【讨论】:

  • 这是特定于 /24 网络的,但也有其他网络大小。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-16
  • 2015-09-15
  • 2019-06-13
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
相关资源
最近更新 更多