-1

Final edit for clarity - In my environment, DNS will only store one record per client. If a client has multiple NICs, or changes subnets, the original IP is registered in DNS until the corresponding DHCP record expires (this is an AD environment where DHCP registers DNS addresses).

In this scenario DNS has one, incorrect, record for the client. I want to query DHCP by client name, to see all IPs that are leased to it.

The only possible solution I have found is to dump all subnet info from DHCP (supported by the below API) then query against that, but that is not feasible in my environment, since multiple people would use this application, and I don't want the additional strain on DHCP.

I cannot change any configuration for DNS or DHCP.

Thanks,


This is similar to this question, but with the referenced API (here), I can only query via IP. Is it possible with this API, or any other, to query DHCP by hostname? (The issue being, DNS gives me an old IP for MachineA, I want to retrieve any other IPs being leased by MachineA from the DHCP server).

Edit: To clarify, I want to write a program that I can type in a hostname, it will then query a DHCP server for all IPs for that hostname in any subnet administered by that DHCP server. This is to workaround the issue of a machine with multiple NICs registering an IP that is useless to me (wireless), so for instance the DNS result may be NICA (wireless) but I want NICB (wired).

Community
  • 1
  • 1
scott.se
  • 70
  • 1
  • 8
  • Have you done a google search on this Scott.se ? try this link http://ianatkinson.net/computing/dhcpcsharp.htm – MethodMan Feb 15 '13 at 17:07
  • It's not clear from you question why something like `Dns.GetHostEntry` doesn't suit your needs. – Peter Ritchie Feb 15 '13 at 17:20
  • @DJKRAZE Hello DJ, if I read this code right, that is a little closer to what I'm trying to do, but not quite the same. It appears that pulls all information for clients in a subnet (which would be unknown to me). I'd like to go as light as possible and only pull information for one client if possible - I'm hoping to make something that can be redistributed so do not want to cause any more strain than necessary – scott.se Feb 15 '13 at 20:39
  • @PeterRitchie Peter, if nslookup returns only one address, would that mean Dns.GetHostEntry would also only return one? I do not have access to the DNS server to see what the records looks like, so not sure what to expect from this method, that is why I was exploring DHCP. – scott.se Feb 16 '13 at 18:34

2 Answers2

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.

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
  • I +1ed this answer for its technical correctness, despite my customary reservations about [asking the client a question they cannot answer](http://blogs.msdn.com/b/oldnewthing/archive/2004/04/26/120193.aspx) :-) – Cody Gray - on strike Feb 15 '13 at 23:30
  • Maybe I missed it, but I'm not sure how the answer applies - I'm not looking for the best path out of a client, I want to view all possible paths INTO a client, by querying the DHCP server, since that will have multiple addresses for the client, whereas DNS would only tell me what is registered. As stated in the question, wireless IP is undesirable and I want the wired IP, but wired/wireless is arbitrary - DNS tells me one IP, I want to know other IPs the device may have since the DNS IP may be undesirable. – scott.se Feb 16 '13 at 03:49
  • I'm not sure how what I posted details "out of a client" (or INTO a client, for that matter). It simply allows you to filter the possible NICs with IP addresses--you can then use those IP addresses to create socket to listen (IN). DNS/DHCP knows nothing of wired/wireless. I'm unclear how you can possibly do what you want with just DHCP/DNS? Regardless, for something to come INTO your server, you must first create a socket to listen on a *valid* IP address, regardless of what DNS/DHCP has cached. – Peter Ritchie Feb 17 '13 at 22:40
  • @CodyGray if that were a criteria for validity to answer questions on SO, almost all questions would have to be closed :) – Peter Ritchie Feb 17 '13 at 22:40
  • @PeterRitchie I mean to say that although I don't follow your answer completely, I do not see how it relates to my issue. It appears to me the snippet is for an application to select an IP based on certain criteria. I cannot apply this to every machine, nor can I apply it to the DNS or DHCP servers. I'm looking for a way to query DHCP using C# for all IPs being leased by a hostname. In my scenario, DNS only registers one IP for a machine, and the only place I know of to see what other IPs it might have is DHCP. – scott.se Feb 18 '13 at 21:31
  • @scott.se DHCP allocates IP addresses to computers--that's independant of the name. A DHCP server *could*, on behalf the the client, inform a DNS server of the allocation; but you'd have to query the DNS for the IP address of a computer name--which leaves your right back where you started. So, I guess the answer you're looking for is "no" and a workaround is to implement the choice in the application (e.g. like what I posted) because the app has specific needs. – Peter Ritchie Feb 18 '13 at 22:48
  • @scott.se a computer "name" is associated to one IP, to find all *other* IPs based on name, doesn't make any sense. You'd have to enumerate all the IPs on the machine and somehow check with the DHCP server if *it* allocated those IPs... But, I think you're asking the wrong question. – Peter Ritchie Feb 18 '13 at 22:50
0

If you are trying to locate a machine on the network, then querying DNS is probably what you want to do first. i.e. Think of a machine that has a static ip address on the network. It would register its name with the name service only, it would not show up in DHCP att all if the machine's IP stack is configured with the static address only.

I'm not sure how long it should take for a new machine or a recently changed IP address to show up in DNS. But if you want to see if DHCP has something different(newer), then query DHCP after trying it from DNS first.

dmarietta
  • 1,940
  • 3
  • 25
  • 32
  • The main issue is that the wireless IP is the one to register in DNS, not the wired IP. These machines cannot be remotely administered over the wireless, so if I only have the name, I cannot remotely administer them. I also cannot change anything on the DNS or DHCP servers, so I'm looking for a way to find the wired IP of a machine via the client name. – scott.se Feb 15 '13 at 17:42