Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all IPs assigned to a server

Tags:

c#

.net

dns

wmi

I came across a code snippet on this answer: Get IP address in a console application

using System; 
using System.Net; 


namespace ConsoleTest 
{ 
    class Program 
    { 
        static void Main() 
        { 
            String strHostName = string.Empty; 
            // Getting Ip address of local machine... 
            // First get the host name of local machine. 
            strHostName = Dns.GetHostName(); 
            Console.WriteLine("Local Machine's Host Name: " + strHostName); 
            // Then using host name, get the IP address list.. 
            IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); 
            IPAddress[] addr = ipEntry.AddressList; 

            for (int i = 0; i < addr.Length; i++) 
            { 
                Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString()); 
            } 
            Console.ReadLine(); 
        } 
    } 
} 

This code works fine when I run it on locally to get the IP addresses of my computer. What I'm trying to do is use the code to get the IP addresses on a server on my network. So basically I tried replacing strHostName = Dns.GetHostName(); with strHostName = "myServerName"; but it only returns one IP. When I run the program on the server itself I get all of the IPs assigned to that server. The goal is to run the program on my computer, read server names from a database table to get the IP addresses on over 100 servers. I'm trying to avoid having to log on to each server and run the program on each one of the servers to get the IP addresses.

Interesting enough the code works fine for "www.google.com" - is this server related or a security issue?

Before I can start getting data from the database I need to get the code to work for one server :) Hope that explains it better. Thanks!

like image 736
nelsonGti Avatar asked Oct 10 '12 02:10

nelsonGti


1 Answers

Are you confusing the difference between what DNS says about the name you are trying to resolve using those methods versus finding out what IP addresses are actually bound to the TCP/IP protocol stack running on the specific machine you are interested in? If you want to know what IP addresses that the machine is actually configured for, regardless of what may or may not be registered in DNS, you would want to look at using WMI for enumerating the addresses configured on the machine.

See article: WMI Query to get IP Address, Domain Name, OS Version

In your code above, you are simply asking the local machine the code is running on, to use its configured DNS resolver and tell you what entries exist as DNS records for the name you are using to query. That is why you can get a value when you try a query for something like www.google.com. It is because you are simply looking up a name in DNS and since that is a publicly recognizable name, any correctly setup DNS server will be able to report a valid value.

This is very different from what IP addresses might be bound to the server. For example, think of a server sitting behind a firewall. The server might actually be using IP Addresses that are local to the internal network which are mapped to publicly accessible IP addresses on the firewall. In that case, the IP addresses the server knows about are completely different from the public IP addresses that would show up in its corresponding DNS entries.

like image 61
dmarietta Avatar answered Nov 02 '22 08:11

dmarietta