繁体   English   中英

如何从 C# 中的 Given IP 获取域名?

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

我想从给定的 IP 获取域名。例如,如果我将 IP 指定为“172.24.17.85”,我应该只获取域名,例如我的域名是 sonata.net。

C# 中的任何代码片段?

你试过Dns.GetHostEntry吗?

例:

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"
    }
}

请注意,这不适用于您提供的示例...如果反向DNS查找不起作用,我不知道您可以做什么。

Console.WriteLine("DomainName: {0}", Dns.GetHostEntry("1.1.1.1").HostName);

我真的怀疑这是否可行。 可能有n个域指向单个IP。 您可以对反向DNS查找进行一些研究。

这是一个基于@Jon Skeets 回答的完整程序,但带有输入/输出文件。

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();
            }
        }
    }
}

输入文件的格式应类似于此示例:

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

output 文件将如下例所示:

10.0.10.126 | hostname123.dhh.lan
10.0.10.17 | hostname863.dhh.lan
10.0.20.120 | Thinkpad16.DHH.LAN
10.0.20.126 | Thinkpad08.DHH.LAN
10.0.20.127 | NUC-H4-CAM.DHH.LAN
10.0.20.138 | Thinkpad04.DHH.LAN
10.0.20.139 | Thinkpad09.DHH.LAN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM