如果主机名(字符串)解析为本地计算机,谁能想到在win32或.NET中告诉的简单方法?如:
"myhostname" "myhostname.mydomain.local" "192.168.1.1" "localhost"
此练习的目标是生成一个测试,该测试将告知Windows安全层是否将对计算机的访问视为本地或网络
在.NET中,您可以:
IPHostEntry iphostentry = Dns.GetHostEntry (Dns.GetHostName ());
然后,对于任何主机名,检查它是否解析为其中一个IP iphostEntry.AddressList
(这是一个IPAddress []).
这是一个完整的程序,它将检查命令行中传递的主机名/ IP地址:
using System; using System.Net; class Test { static void Main (string [] args) { IPHostEntry iphostentry = Dns.GetHostEntry (Dns.GetHostName ()); foreach (string str in args) { IPHostEntry other = null; try { other = Dns.GetHostEntry (str); } catch { Console.WriteLine ("Unknown host: {0}", str); continue; } foreach (IPAddress addr in other.AddressList) { if (IPAddress.IsLoopback (addr) || Array.IndexOf (iphostentry.AddressList, addr) != -1) { Console.WriteLine ("{0} IsLocal", str); break; } } } } }