0

I have this method for getting IP v4 of an IIS server by its computer name, every time I run this function, it gives me the different result (it alternate between 4 different IP addresses, three of them are in the same range and one is in range 192.168.x.x)

   public static string GetIpFromPcName(string PcName = null)
        {
            try
            {
                // in order to get ip v4 address 
                // look here: https://stackoverflow.com/questions/6668810/how-do-i-determine-the-local-host-s-ipv4-addresses
                // can a pc have multiple ips?

                if (string.IsNullOrEmpty(PcName))
                    return null;

                string IP4Address = String.Empty;
                var hostEntry = Dns.GetHostEntry(PcName);
                var ipaddresses = Dns.GetHostAddresses(PcName);
                Console.WriteLine("HostName: {0}", hostEntry.HostName);
                Console.WriteLine("Aliases:");
                foreach (var entry in hostEntry.Aliases)
                {
                    Console.WriteLine("\t{0}", entry);
                }
                Console.WriteLine("Addresslist: ");
                foreach (var entry in hostEntry.AddressList)
                {
                    Console.WriteLine("\t{0}", entry.ToString());
                }
                foreach (IPAddress IPA in Dns.GetHostAddresses(PcName))
                {
                    if (IPA.AddressFamily == AddressFamily.InterNetwork)
                    {
                        IP4Address = IPA.ToString();
                        break;
                    }
                }

                return IP4Address;

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in getting ip address for {0}", PcName);
                return null;
            }
        }

Also, one strange thing that I observed is that what gets printed for Addresslist is different from the final return value of the function.

The question is...

why I'm getting different results every time?

shami sheikh
  • 552
  • 3
  • 13
  • 26
Rathma
  • 1,196
  • 2
  • 33
  • 65
  • You can open a CMD and enter ipconfig on Windows or ifconfig on Linux to check if those are the same – Evertude Aug 11 '18 at 09:35
  • I don't have access to that server ... but I checked by using `Dns.GetHostEntry(IPAddress.Parse("IpAddress")).HostName` and they returned the same host name. – Rathma Aug 11 '18 at 09:37
  • BTW: Consider using string interpolation like Console.WriteLine(**$**"HostName: {hostEntry.HostName}"); – Evertude Aug 11 '18 at 09:59
  • Is the site hosted on a load balanced cluster? One "site" doesn't necessarily mean one server. – Crowcoder Aug 11 '18 at 11:33
  • I really don't know that, I should ask, if the answer is yes , what is the implication? – Rathma Aug 11 '18 at 11:48
  • @Rachmaninoff whether the answer is "yes", or there are 4 nics as Evertude surmises, you are going to have to deal with it. You have not mentioned *why* you need to know the IP address so I don't know the implication. – Crowcoder Aug 11 '18 at 12:40

1 Answers1

0

The reason why you are getting different results is because your Server seems to have 4 different network interfaces with different IP addresses. Since your function only returns the first one it finds and the order seems to be random, you will always get one of the 4 addresses of your server

Evertude
  • 184
  • 10