查询 dns 别名

发布于 2024-08-05 23:02:16 字数 2395 浏览 4 评论 0原文

我找到了一些代码来自 msdn 站点(下面包含代码),看起来它将返回给定服务器的所有 dns 别名。我已经在控制台应用程序中实现了代码,它应该允许我输入服务器的主机名,并且它应该返回所有 dns 别名。我输入了我们域中已知具有别名的服务器的主机名(我可以 ping 主机和别名,它们都解析为相同的 IP),但此代码找不到别名。显然我对 dns 别名和/或代码的理解缺乏......请教育我......

static void Main(string[] args)
{
    Console.Write("Host? (Enter for local): ");
    string strHost = Console.ReadLine();
    if (strHost.Trim().Length == 0)
    {
        strHost = System.Net.Dns.GetHostName();
    }

    try
    {
        //System.Net.IPAddress hostIPAddress = System.Net.IPAddress.Parse(strHost);
        System.Net.IPHostEntry hostInfo = System.Net.Dns.GetHostByName(strHost);//.GetHostByAddress(hostIPAddress);
        // Get the IP address list that resolves to the host names contained in 
        // the Alias property.
        System.Net.IPAddress[] address = hostInfo.AddressList;
        // Get the alias names of the addresses in the IP address list.
        String[] alias = hostInfo.Aliases;

        Console.WriteLine("Host name : " + hostInfo.HostName);
        Console.WriteLine("\nAliases :");
        for (int index = 0; index < alias.Length; index++)
        {
            Console.WriteLine(alias[index]);
        }
        Console.WriteLine("\nIP address list : ");
        for (int index = 0; index < address.Length; index++)
        {
            Console.WriteLine(address[index]);
        }
    }
    catch (System.Net.Sockets.SocketException e)
    {
        Console.WriteLine("SocketException caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
    }
    catch (FormatException e)
    {
        Console.WriteLine("FormatException caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
    }
    catch (ArgumentNullException e)
    {
        Console.WriteLine("ArgumentNullException caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
    }
    catch (Exception e)
    {
        Console.WriteLine("Exception caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
    }

    Console.WriteLine("Any key to continue...");
    Console.ReadKey();
}

I found some code from the msdn site (Code included below), which looks like it will return all dns aliases for a given server. I've implemented the code in a cosole app, which should allow me to enter the host name of a server and it should return all dns alias names. I enter the host name of a server in our domain known to have aliases (I can ping the host and the aliases and they all resolve to the same IP), but this code does not find the alias names. Obvously my understanding of dns aliases and/or the code is lacking... please educate me...

static void Main(string[] args)
{
    Console.Write("Host? (Enter for local): ");
    string strHost = Console.ReadLine();
    if (strHost.Trim().Length == 0)
    {
        strHost = System.Net.Dns.GetHostName();
    }

    try
    {
        //System.Net.IPAddress hostIPAddress = System.Net.IPAddress.Parse(strHost);
        System.Net.IPHostEntry hostInfo = System.Net.Dns.GetHostByName(strHost);//.GetHostByAddress(hostIPAddress);
        // Get the IP address list that resolves to the host names contained in 
        // the Alias property.
        System.Net.IPAddress[] address = hostInfo.AddressList;
        // Get the alias names of the addresses in the IP address list.
        String[] alias = hostInfo.Aliases;

        Console.WriteLine("Host name : " + hostInfo.HostName);
        Console.WriteLine("\nAliases :");
        for (int index = 0; index < alias.Length; index++)
        {
            Console.WriteLine(alias[index]);
        }
        Console.WriteLine("\nIP address list : ");
        for (int index = 0; index < address.Length; index++)
        {
            Console.WriteLine(address[index]);
        }
    }
    catch (System.Net.Sockets.SocketException e)
    {
        Console.WriteLine("SocketException caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
    }
    catch (FormatException e)
    {
        Console.WriteLine("FormatException caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
    }
    catch (ArgumentNullException e)
    {
        Console.WriteLine("ArgumentNullException caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
    }
    catch (Exception e)
    {
        Console.WriteLine("Exception caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
    }

    Console.WriteLine("Any key to continue...");
    Console.ReadKey();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

━╋う一瞬間旳綻放 2024-08-12 23:02:16

对于 DNS 名称,只有当您查询的名称具有 CNAME 记录时,别名列表才会非空;然后,别名列表将为您提供所有必须解析才能获得最终名称的 CNAME。

考虑以下问题:

  • 不可能(即协议不支持)找出给定名称的所有 CNAME。这是不可行的,因为它需要搜索整个全球 DNS。
  • 别名不仅可能由 CNAME 产生,也可能由具有相同地址 (A) 记录的多个主机名产生。在这种情况下,并不是一个是另一个的别名,而是它们只是指向同一个 IP 地址。同样,该协议不支持查找某个 IP 地址的所有 A 记录(尽管反向查找可能会找到一些)。

For a DNS name, the list of aliases will only be non-empty if the name you are querying has a CNAME record; then, the alias list will give you all CNAMEs that had to be resolved in order to get the ultimate name.

Consider the following problems:

  • it is not possible (i.e. the protocol does not support it) to find out all CNAMEs for a given name. This is infeasible, as it would require searching the entire global DNS.
  • aliasing may not only occur by CNAMEs, but can also happen by multiple host names having the same address (A) record. In this case, it's not that one is alias of another, but they just point to the same IP address. Again, the protocol doesn't support finding all A records for an IP address (although reverse lookup may find some).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文