在当前子网中查找免费 IP 地址的最简单方法?

发布于 2024-08-21 22:11:32 字数 260 浏览 8 评论 0原文

在本地适配器子网上查找下一个未使用的 IP 地址的最简单方法是什么?

IPAddress GetNextFreeIP(NetworkInterface intface)
{
    ...?
}

更新
我编写了一个 BOOTP/TFTP 服务器,可用于许多不同的场景(交叉电缆、小型专用网络、大型企业网络)。我必须给我正在更新的小型嵌入式系统一个IP地址,并想找到一个备用的供它使用......

What is the easiest way to find the next unused IP address on a local adaptors subnet?

IPAddress GetNextFreeIP(NetworkInterface intface)
{
    ...?
}

Update
I've written a BOOTP/TFTP server that could be used in many different scenarios (cross-over cable, small private network, large corporate network). I have to give the small embedded system I'm updating an IP address and would like to find a spare one for it to use...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

网名女生简单气质 2024-08-28 22:11:32

如果您有 DHCP 管理的 IP 范围,则您有一个可以与之通信的实例 - DHCP 服务器(如 David 推荐的)

如果您没有托管的 IP 范围并且没有其他可以与之通信的实例,则没有可靠的方法可以与判断 IP 是否已使用或未使用。 IP 本身并不提供此类服务。

If you have DHCP-managed IP ranges, you have an instance you can talk to - the DHCP server (like David recommended)

If you don't have managed IP ranges and no other instance you can talk to, there is no reliable way to tell if an IP is used or unused. IP does not offer such a service itself.

零度° 2024-08-28 22:11:32

除了询问 DHCP 服务器之外,我想您还可以在子网上执行广播 ping 操作并收集所有计算机的响应,然后对 IP 地址进行简单排序以查找下一个。当然,这是假设所有设备都在线并且响应广播。所以在一个相当受控的环境中,这是可行的。

Apart from asking the DHCP-server, I guess you could do a broadcast ping on the subnet and collect the responses from all computers and simply sort the IP-addresses to find the next one. This assumes, of course, that all devices are on-line and responds to broadcasts. So in a pretty controlled environment, this could work.

隔纱相望 2024-08-28 22:11:32

简而言之,正如 Thorsten79 刚才所说,你不能。

一个稍长的答案:

这取决于您的网络配置:管理员决定如何分配 IP 地址。通常,有一组为服务器、路由器等手动分配的 IP 地址,以及一组为工作站等分配的 dhcp IP 地址。

如果您可以与 dhcp 服务器通信,您可能会发现保留范围中的哪些地址是空闲的,但对于其余地址,您无法找到。

更有趣的问题是你想要实现什么目标?或许可以用不同的方式来实现?

The short answer is you can't, as Thorsten79 just said.

A slightly longer answer:

It depends on your network's configuration: how the admin has decided to allocate ip addresses. Usually there's a mix of manually assigned ip addresses for servers, routers etc, and a set of dhcp assigned ip addresses for workstations and such.

If you can talk to the dhcp server you might find out which addresses in the reserved range are free, but for the rest of the addresses you cannot find out.

The more interesting question is what you are trying to accomplish? Perhaps it can be realized in a different manner?

苏别ゝ 2024-08-28 22:11:32

根据我对带有 TFTP 服务器的嵌入式设备的观察,它们使用记录的硬编码 IP 地址启动。经过一段设定的时间(约 3-10 秒)后,引导加载程序将控制权转移给读取其配置并设置 IP 地址的应用程序。

如果有人想要使用引导加载程序来加载新固件,那么他们需要阅读文档,确保 IP 地址可访问(在同一子网上),重新启动设备,并将应用程序通过 TFTP 传输到设备。

From what I've seen of embedded devices with a TFTP server, they boot up with a hard coded IP Address that is documented. After a set period of time (~3-10 seconds), the boot loader transfers control to the application that reads its configuration and sets the IP Address.

If someone wants to use the boot loader to load new firmware, then they need to read the documentation, make sure that IP Address is reachable (on the same subnet), reboot the device, and TFTP the application to the device.

扭转时空 2024-08-28 22:11:32

当您说无法确保 IP 不被使用时,您在技术上是正确的。但我现在决定依靠这个......

    public static IPAddress FindNextFree(this IPAddress address)
    {
        IPAddress workingAddress = address;
        Ping pingSender = new Ping();

        while (true)
        {
            byte[] localBytes = workingAddress.GetAddressBytes();

            localBytes[3]++;
            if (localBytes[3] > 254)
                localBytes[3] = 1;

            workingAddress = new IPAddress(localBytes);

            if (workingAddress.Equals(address))
                throw new TimeoutException("Could not find free IP address");

            PingReply reply = pingSender.Send(workingAddress, 1000);
            if (reply.Status != IPStatus.Success)
            {
                return workingAddress;
            }
        }
    }

You are all technically right when you say there is no way to ensure an IP isn't being used. But I've decided to rely on this for now...

    public static IPAddress FindNextFree(this IPAddress address)
    {
        IPAddress workingAddress = address;
        Ping pingSender = new Ping();

        while (true)
        {
            byte[] localBytes = workingAddress.GetAddressBytes();

            localBytes[3]++;
            if (localBytes[3] > 254)
                localBytes[3] = 1;

            workingAddress = new IPAddress(localBytes);

            if (workingAddress.Equals(address))
                throw new TimeoutException("Could not find free IP address");

            PingReply reply = pingSender.Send(workingAddress, 1000);
            if (reply.Status != IPStatus.Success)
            {
                return workingAddress;
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文