我从 msdn 站点找到了一些代码(代码包含在下面),看起来它将返回给定服务器的所有 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();
}