使用DNS 轻松获取主机信息
利用dns類和WMI規(guī)范獲取IP及MAC地址
?在C#編程中,要獲取主機(jī)名和主機(jī)IP地址,是比較容易的.它提供的Dns類,可以輕松的取得主機(jī)名和IP地址.
?
?示例:
?string strHostName = Dns.GetHostName();? //得到本機(jī)的主機(jī)名
?IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本機(jī)IP
?string strAddr = ipEntry.AddressList[0].ToString(); //假設(shè)本地主機(jī)為單網(wǎng)卡
?
?在這段代碼中使用了兩個(gè)類,一個(gè)是Dns類,另一個(gè)為IPHostEntry類,二者都存在于命名空間System.Net中.
?Dns類主要是從域名系統(tǒng)(DNS)中檢索關(guān)于特定主機(jī)的信息,上面的代碼第一行就從本地的DNS中檢索出本地主機(jī)名.
?IPHostEntry類則將一個(gè)域名系統(tǒng)或主機(jī)名與一組IP地址相關(guān)聯(lián),它與DNS類一起使用,用于獲取主機(jī)的IP地址組.
?要獲取遠(yuǎn)程主機(jī)的IP地址,其方法也是大同小異.
?
?在獲取了IP地址后,如果還需要取得網(wǎng)卡的MAC地址,就需要進(jìn)一步探究了.
?這里又分兩種情況,一是本機(jī)MAC地址,二是遠(yuǎn)程主機(jī)MAC地址.二者的獲取是完全不同的.
?在獲取本機(jī)的MAC地址時(shí),可以使用WMI規(guī)范,通過SELECT語句提取MAC地址.在.NET框架中,WMI規(guī)范的實(shí)現(xiàn)定義在System.Management命名空間中.
?ManagementObjectSearcher類用于根據(jù)指定的查詢檢索管理對象的集合
?ManagementObjectCollection類為管理對象的集合,下例中由檢索對象返回管理對象集合賦值給它.
?
?示例:
?ManagementObjectSearcher query =new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration") ;
?ManagementObjectCollection queryCollection = query.Get();
?foreach( ManagementObject mo in queryCollection )
?{
??if(mo["IPEnabled"].ToString() == "True")
??mac = mo["MacAddress"].ToString();
?}
?
?獲取遠(yuǎn)程主機(jī)的MAC地址時(shí),需要借用API函數(shù)SendARP.該函數(shù)使用ARP協(xié)議,向目的主機(jī)發(fā)送ARP包,利用返回并存儲在高速緩存中的IP和MAC地址對,從而獲取遠(yuǎn)程主機(jī)的MAC地址.
?
?示例:
?Int32 ldest= inet_addr(remoteIP); //目的ip
?Int32 lhost= inet_addr(localIP); //本地ip
?
?try
?{
??Int64 macinfo = new Int64();
??Int32 len = 6;
??int res = SendARP(ldest,0, ref macinfo, ref len);? //發(fā)送ARP包
??return Convert.ToString(macinfo,16);
?}
?catch(Exception err)
?{
??Console.WriteLine("Error:{0}",err.Message);
?}
?return 0.ToString();
?
?但使用該方式獲取MAC時(shí)有一個(gè)很大的限制,就是只能獲取同網(wǎng)段的遠(yuǎn)程主機(jī)MAC地址.因?yàn)樵跇?biāo)準(zhǔn)網(wǎng)絡(luò)協(xié)議下,ARP包是不能跨網(wǎng)段傳輸?shù)?#xff0c;故想通過ARP協(xié)議是無法查詢跨網(wǎng)段設(shè)備MAC地址的。
?
?示例程序:
?
?using System.Net;
?using System;
?using System.Management;
?using System.Runtime.InteropServices;
?
?public class getIP
?{
??[DllImport("Iphlpapi.dll")]
??private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32 length);
??[DllImport("Ws2_32.dll")]
??private static extern Int32 inet_addr(string ip);
?
??//獲取本機(jī)的IP
??public string getLocalIP()
??{
???string strHostName = Dns.GetHostName();? //得到本機(jī)的主機(jī)名
???IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本機(jī)IP
???string strAddr = ipEntry.AddressList[0].ToString();
???return(strAddr);
??}
??//獲取本機(jī)的MAC
??public string getLocalMac()
??{
???string mac = null;
???ManagementObjectSearcher query =new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration") ;
???ManagementObjectCollection queryCollection = query.Get();
???foreach( ManagementObject mo in queryCollection )
???{
????? if(mo["IPEnabled"].ToString() == "True")
??????? mac = mo["MacAddress"].ToString();
???}
???return(mac);
??}
?
??//獲取遠(yuǎn)程主機(jī)IP
??public string[] getRemoteIP(string RemoteHostName)
??{
???IPHostEntry ipEntry = Dns.GetHostByName(RemoteHostName);
???IPAddress[] IpAddr = ipEntry.AddressList;
???string[] strAddr = new string[IpAddr.Length];
???for (int i=0;i ???{
????strAddr[i] = IpAddr[i].ToString();
???}
???return(strAddr);
??}
??//獲取遠(yuǎn)程主機(jī)MAC
??public string getRemoteMac(string localIP, string remoteIP)
??{
???Int32 ldest= inet_addr(remoteIP); //目的ip
???Int32 lhost= inet_addr(localIP); //本地ip
??
???try
???{
????Int64 macinfo = new Int64();
????Int32 len = 6;
????int res = SendARP(ldest,0, ref macinfo, ref len);
????return Convert.ToString(macinfo,16);
???}
???catch(Exception err)
???{
????Console.WriteLine("Error:{0}",err.Message);
???}
???return 0.ToString();
??}
??
??
??public static void Main(string[] args)
??{
???getIP gi = new getIP();
???Console.WriteLine("本地網(wǎng)卡信息:");
???Console.WriteLine(gi.getLocalIP() + " - " + gi.getLocalMac());
???
???Console.WriteLine("\n\r遠(yuǎn)程網(wǎng)卡信息:");
???string[] temp = gi.getRemoteIP("scmobile-tj2");
???for(int i=0;i ???{
???????? Console.WriteLine(temp[i]);
???}
???Console.WriteLine(gi.getRemoteMac("192.168.0.3","192.168.0.1"));
??}
?}
轉(zhuǎn)載于:https://www.cnblogs.com/Fly-sky/archive/2008/08/18/1270315.html
總結(jié)
以上是生活随笔為你收集整理的使用DNS 轻松获取主机信息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab求解多项式系数,C++和MA
- 下一篇: 怎么查MATLAB中的newrbf,已经