13

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#?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Swapnil Gupta
  • 8,751
  • 15
  • 57
  • 75

4 Answers4

33

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1
Console.WriteLine("DomainName: {0}", Dns.GetHostEntry("1.1.1.1").HostName);
KMån
  • 9,896
  • 2
  • 31
  • 41
  • Hi Jon, Your Code works fine but the problem is when i give any external Ip or my own IP i get the Domain name but if try to give IP of any machine which is present inlocal network it returns me the IP address instead of Domain name. Pls help me.... – Swapnil Gupta Jul 15 '10 at 06:21
  • @Swapnil: Try a reverse lookup from commandline `nslookup [ip address]` and see what you get for that ip address. Also, you can try using `Dns.GetHostAddresses`; http://msdn.microsoft.com/en-us/library/system.net.dns.gethostaddresses.aspx – KMån Jul 15 '10 at 07:11
  • using this at command prompt gives me some other address and if i use let address it gives me domain name. So what's the problem i am not able to understand ? – Swapnil Gupta Jul 15 '10 at 07:31
1

I really doubt if this is going to be possible. There could be n number of domains pointing to a single IP. You can do some research on Reverse DNS lookup.

this. __curious_geek
  • 42,787
  • 22
  • 113
  • 137
0

Here is a complete program based on @Jon Skeets answer, but with input/output file.

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace IP_reverse_checker
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // based on source: https://stackoverflow.com/a/44040867/6584859
            // based on source: https://stackoverflow.com/a/3252871

            Regex ip = new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");

            Console.WriteLine(">>> IP reverse checker <<<");
            Console.WriteLine();
            Console.WriteLine("Please enter the complete path to the text file containing the list of IP addresses:");
            string fileNameInput = @"" + Console.ReadLine();

            // Lets remove leading/trailing double quotes, if present:
            char[] trimChars = { '"' };
            string cleanFileNameInput = fileNameInput.Trim(trimChars);

            int numberFileLines = 0;
            string[] inputLines = new string[] { };

            try
            {
                inputLines = File.ReadAllLines(cleanFileNameInput);
                numberFileLines = inputLines.Length;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error reading the file!  error: " + ex.Message);
                Console.WriteLine();
                Console.WriteLine("Press ENTER to exit the program.");
                Console.ReadLine();
                Environment.Exit(0);
            }

            List<string> outputLines = new List<string>();

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("The file contains " + numberFileLines + " lines. I start processing...");
            Console.WriteLine();

            int counter = 0;

            foreach (string line in inputLines)
            {
                counter++;

                try
                {
                    MatchCollection result = ip.Matches(line);

                    if (result[0].Success)
                    {
                        string hostName = "";
                        IPAddress addr = IPAddress.Parse(line);

                        try
                        {
                            IPHostEntry entry = Dns.GetHostEntry(addr);
                            hostName = entry.HostName;
                        }
                        catch (Exception)
                        {
                            hostName = ">> No Reverse-DNS entry found!";
                        }

                        if (hostName != "")
                        {
                            outputLines.Add(line + " | " + hostName);
                            Console.WriteLine("line " + counter + " of " + numberFileLines + ": " + line + " | " + hostName);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error processing line " + counter + "  -  error: " + ex.Message);
                }
            }

            if (outputLines.Count > 0)
            {
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("Done! " + outputLines.Count + " IP adresses were processed.");
                Console.WriteLine();
                Console.WriteLine("Please enter the complete path to the text file where the list with IP addresses AND hostnames should be stored:");

                string fileNameOutput = @"" + Console.ReadLine();
                string cleanFileNameOutput = fileNameOutput.Trim(trimChars);

                try
                {
                    File.AppendAllLines(cleanFileNameOutput, outputLines);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error writing the file!  error: " + ex.Message);
                }

                Console.WriteLine();
                Console.WriteLine("Done! Output file was written. Please press ENTER to exit the program.");
                Console.ReadLine();
            }
        }
    }
}

The input file should be in a format like this example:

10.0.10.126
10.0.10.17
10.0.20.120
10.0.20.126
10.0.20.127
10.0.20.138
10.0.20.139

The output file will look like this example:

10.0.10.126 | Thinkpad23.dnssuffix
10.0.10.17 | desktop63.dnssuffix
10.0.20.120 | Thinkpad16.dnssuffix
10.0.20.126 | Thinkpad08.dnssuffix
10.0.20.127 | desktop19.dnssuffix
10.0.20.138 | Thinkpad04.dnssuffix
10.0.20.139 | Thinkpad09.dnssuffix
RebootDeluxe
  • 762
  • 3
  • 16