Get all IP addresses assigned to the server

To this answer, I found a code snippet: Get the 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 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 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 returns only one IP address. When I run the program on the server itself, I get all the IP addresses assigned to this server. The goal is to run the program on my computer, read the server names from the database table to obtain IP addresses on more than 100 servers. I try to avoid having to log on to each server and run the program on each of the servers to get IP addresses.

Interestingly enough, the code works great for "www.google.com" - is this server or a security issue?

Before I can start getting data from the database, I need the code to work on one server :) I hope this explains it better. Thanks!

+6
source share
2 answers

Do you mix the difference between what DNS says about the name you are trying to resolve using these methods and find out which IP addresses are actually bound to the TCP / IP protocol stack that runs on the particular machine in which you are interested? If you want to know what IP addresses the machine is configured to, regardless of what may or may not be registered in the DNS, you would like to look at using WMI to list the addresses configured on the machine.

See article: WMI Request for IP Address, Domain Name, OS Version

In the above code, you simply ask the local machine that the code is running to use the configured DNS resolver and indicate which records exist as DNS records for the name that you use for the query. This is why you can get the value when you try to query for something like www.google.com. This is because you are simply looking for a name in DNS and, since it is a public name, any properly configured DNS server will be able to report a valid value.

This is very different from what IP addresses can be bound to the server. For example, think of a server sitting behind a firewall. In fact, the server can use IP addresses that are local to the internal network, which map to the public IP addresses on the firewall. In this case, the IP addresses that the server knows about are completely different from the public IP addresses that will be displayed in the corresponding DNS records.

+3
source

It is possible that other IP addresses were excluded from DNS specifically for the server. A good example of this functionality can be found in this article (975808) KB .

Try GetHostAddress() and make sure you get the same result. See MSDN: gethostaddress ()

Otherwise, you can look at other methods for querying IP addresses.

0
source

Source: https://habr.com/ru/post/927313/


All Articles