Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get domain name from Given IP in C#?

Tags:

c#

I want to get domain name from a given IP. E.g If I give IP as "172.24.17.85" I should get only domain name like my domain name is sonata.net.

Any code snippet for this in C#?

like image 776
Swapnil Gupta Avatar asked Jul 15 '10 06:07

Swapnil Gupta


People also ask

How do I find a domain name from an IP address?

Querying DNS Click the Windows Start button, then "All Programs" and "Accessories." Right-click on "Command Prompt" and choose "Run as Administrator." Type "nslookup %ipaddress%" in the black box that appears on the screen, substituting %ipaddress% with the IP address for which you want to find the hostname.

Can we get hostname from IP?

Method 2: Using nslookup command This is another method to get the hostname from the IP address. Run the nslookup command with an IP address from which you want to get the hostname. This command works a bit differently from the ping command that is discussed above.

How do I find my domain range and IP?

To find the IP address range of a domain, you can use the "dig" or "nslookup" command. For example, if you want to find the IP address range of the domain "example.com", you would use the following command: dig +short example.com The output of this command would be the IP address range of the domain "example.com".

How do you find IP of a domain in CMD?

To do this, open the Command Prompt and type "ping" followed by the domain name. For example, to find the IP address for the website "example.com," you would type "ping example.com."


2 Answers

Have you tried Dns.GetHostEntry?

Example:

using System;
using System.Net;

class Test
{
    static void Main(string[] args)
    {
        IPAddress addr = IPAddress.Parse("69.59.196.211");
        IPHostEntry entry = Dns.GetHostEntry(addr);
        Console.WriteLine(entry.HostName); // Prints "stackoverflow.com"
    }
}

Note that this didn't work for the example you gave... if a reverse DNS lookup doesn't work, I'm not sure what you can do.

like image 153
Jon Skeet Avatar answered Oct 11 '22 22:10

Jon Skeet


Console.WriteLine("DomainName: {0}", Dns.GetHostEntry("1.1.1.1").HostName);
like image 20
KMån Avatar answered Oct 11 '22 20:10

KMån