-1

为清楚起见进行最终编辑 - 在我的环境中,DNS 将只为每个客户端存储一条记录。如果一个客户端有多个网卡,或者更改了子网,那么原始 IP 会在 DNS 中注册,直到对应的 DHCP 记录过期(这是 DHCP 注册 DNS 地址的 AD 环境)。

在这种情况下,DNS 有一个不正确的客户端记录。我想通过客户端名称查询 DHCP,以查看租用给它的所有 IP。

我发现的唯一可能的解决方案是从 DHCP 转储所有子网信息(由下面的 API 支持)然后查询,但这在我的环境中是不可行的,因为多人会使用这个应用程序,我不想要DHCP 的额外压力。

我无法更改 DNS 或 DHCP 的任何配置。

谢谢,


这与问题类似,但使用引用的 API (here),我只能通过 IP 进行查询。是否可以使用此 API 或任何其他 API 按主机名查询 DHCP?(问题是,DNS 为我提供了 MachineA 的旧 IP,我想从 DHCP 服务器检索 MachineA 租用的任何其他 IP)。

编辑:为了澄清,我想编写一个可以输入主机名的程序,然后它将在 DHCP 服务器中查询该 DHCP 服务器管理的任何子网中该主机名的所有 IP。这是为了解决具有多个 NIC 的机器注册一个对我无用的 IP(无线)的问题,例如 DNS 结果可能是 NICA(无线)但我想要 NICB(有线)。

4

2 回答 2

2

From what I can tell, you've encountered the age-old problem of which IP address to use. Now-a-days many computers have multiple NICs, some virtual, some local-only, some with internet access, etc... For the application to choose is very difficult. Most of the time I simply make the IP by which the application hosts things like sockets a configuration item--simply because the application is incapable of really choosing which is the right ip address to use. e.g. two NICs both with the same network access, which do you choose? If you run the application twice, maybe one should use NIC 1 and the other should use NIC 2--how would the app make that determination? (i.e. it can't).

Having said that, depending your needs, you can go looking for the best NIC and get it's IP address. For example, if you want an IPv4 address on a non-wireless NIC, you can do something like:

var ips = from ni
                in NetworkInterface.GetAllNetworkInterfaces()
            where ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet
            from ip in ni.GetIPProperties().UnicastAddresses
            where ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && ip.IsDnsEligible
            select ip;
IPAddress address = ips.First().Address;

...error checking omitted for readability--apply whatever error checking suitable for your requirements.

You can even go so far as to check whether the address is link local (i.e. can communicate out of the local network segment--which usually means an address automatically assigned by Windows instead of DNS/DHCP) by seeing if the first two bytes of an IPv4 address are 169 and 254.

But, you need to specifically define what your requirements are. simply to say "undesirable wireless IP" doesn't provide unambiguous and verifiable criteria to tell what solution will always work for your needs.

于 2013-02-15T21:26:09.387 回答
0

如果您试图在网络上定位一台机器,那么查询 DNS 可能是您首先要做的事情。即想象一台在网络上具有静态IP 地址的机器。它只会在名称服务中注册其名称,如果机器的 IP 堆栈仅配置为静态地址,则它根本不会显示在 DHCP 中。

我不确定新机器或最近更改的 IP 地址需要多长时间才能显示在 DNS 中。但是,如果您想查看 DHCP 是否有不同(较新)的内容,请先从 DNS 尝试后查询 DHCP。

于 2013-02-15T17:15:41.967 回答