7

I am working on a c# console application, and inside my console application, i have the following code, to get the IP for a website:-

using System.Net
using System.Web;
using System.IO;

namespace MyConsoleApp
{
    class Program
    {
      static async Task Main(string[] args)
        {
          IPHostEntry hosten = Dns.GetHostEntry("www." + website);
          if (hosten.AddressList.Count() >= 1)
              {
               string ip = hosten.AddressList[0].ToString();

but the IP i will get for the website will be different compared to the IP i will get from some online IP checker sites such as https://www.site24x7.com/find-ip-address-of-web-site.html.. so is using System.Net.Dns.GetHostEntry() a trusted appraoch to know the IP address for a web site? if the answer is yes then why i am getting different IP from online IP checker sites?

John John
  • 1
  • 72
  • 238
  • 501
  • 2
    Some domains have multiple IPs associated with them, and you are only looking at the first one. Take a look at *all* of the entries in `hosten.AddressList`, and see if any of them match your website. – canton7 Oct 28 '19 at 17:00

4 Answers4

5

It's very common for websites to have multiple IP addresses. A website could be distributed across multiple places and regions, and the use of multiple IP addresses helps in load balancing and network traffic optimization, among other things. A common technique is to route requests to the closest IP(s) (although active load balancing could prevent it), hence, an online IP checker could produce the IP(s) closer to it.

Why? Because DNS is a hierarchical distributed regional database, Google could have an IP (or more) address(es) in China and that means it'll get registered mainly in that region's DNS servers. Therefore, naive queries for Google's IP coming from China will produce that/those IP(s), while queries from Australia will produce another/other IP(s).

To make things more complicated, Content Delivery Networks like Cloudflare expose an IP of the website which is not its real IP. They do it as part of their DDoS mitigation and overall web security strategies.

All of the above makes it difficult to get a list that contains a complete and universal list of the IP addresses of a particular website. However, if you query the same DNS servers from the same place, you should get a fairly constant list of IP addresses assigned to that host.

Now, regarding your code, when you do:

hosten.AddressList[0]

you're taking only the first IP address in that list, which could be the only IP if that particular host had only one, but could be the first of several resolved IPs of that host. You must check all of the elements of AddressList.

Javier Silva Ortíz
  • 2,864
  • 1
  • 12
  • 21
3

Many websites will have multiple IPs, as they're hosted in multiple places. For further reading check out "Content Delivery Networks"

This is a lot like asking "Why does Pizza Hut have a lot of phone numbers?" Because even though they serve up the same thing, there are a lot of them, and you're going to want to talk to the one closest to you.

Joe Cullinan
  • 552
  • 3
  • 15
0

Simply if your run your code on the same IP area of https://www.site24x7.com/find-ip-address-of-web-site.html you will get the same result, both above answers are correct and have the technical explanation for that.

Khaled Obaid
  • 275
  • 3
  • 13
0

Your answer is : With natural dns query You use your local computer dns server. But you cannot sure that web sites use another dns server for lookup IP from thousands of dns server. Example I used Alternate DNS 198.101.242.72 23.253.163.53 for query correct result same as that web site!

Please add DnsClient Nuget package to your application. Because for professional dns query you need to use detail of something, example dns server. normal query you dont have this chance.

        static void Main(string[] args)
        {
            var client = new LookupClient(new IPEndPoint(IPAddress.Parse("198.101.242.72"),53));
            var result = client.Query("www.nasill.com", QueryType.A);

            foreach (var aRecord in result.Answers.ARecords())
            {
                Console.WriteLine("Correct Result is: "+aRecord.Address);
            }

            IPHostEntry hosten = Dns.GetHostEntry("www.nasill.com");
            if (hosten.AddressList.Count() >= 1)
            {
                string ip = hosten.AddressList[0].ToString();
                Console.WriteLine("Your computer dns says: "+ip);
            }

            Console.ReadLine();

        }
wikiCan
  • 449
  • 3
  • 14