查询dns别名

时间:2009-09-24 22:59:52

标签: c# .net dns alias

我从msdn网站找到了一些code(下面包含的代码),看起来它将返回给定服务器的所有dns别名。我已经在cosole应用程序中实现了代码,这应该允许我输入服务器的主机名,它应该返回所有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();
}

1 个答案:

答案 0 :(得分:6)

对于DNS名称,如果您要查询的名称具有CNAME记录,则别名列表将仅为非空;然后,别名列表将为您提供必须解析的所有CNAME,以获得最终名称。

考虑以下问题:

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