2009-09-24 86 views
4

我從msdn站點(代碼如下)發現了一些code,看起來它會返回給定服務器的所有DNS別名。我已經在cosole應用程序中實現了代碼,應該允許我輸入服務器的主機名,並且它應該返回所有的dns別名。我輸入已知有別名的域中的服務器的主機名(我可以ping主機和別名,它們都解析爲相同的IP),但是此代碼未找到別名。 Obvously我的DNS別名和/或代碼缺乏瞭解......請教育我......查詢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(); 
} 

回答

4

對於DNS名稱,別名的列表將僅在名稱要查詢爲非空有一個CNAME記錄;那麼,別名列表將爲您提供所有必須解析的CNAME以獲取最終名稱。

考慮以下問題:

  • 這是不可能的(即協議不支持的話),找出所有的CNAME給定名稱。這是不可行的,因爲它需要搜索整個全球DNS。
  • 別名不僅可以通過CNAME發生,也可以通過具有相同地址(A)記錄的多個主機名發生。在這種情況下,它不是另一個的別名,而是指向相同的IP地址。同樣,協議不支持查找IP地址的所有A記錄(儘管反向查找可能會找到一些記錄)。