获得局域网中计算机的列表(包括计算机名,IP和MAC)的方法

有的时候需要根据MAC来限定登录的计算机,为此查找了不少资料(有来自博客堂和CSDN),下面是获得远程计算机的MAC和局域网中计算机列表的方法。


需要引用的命名空间 

None.gif using  System; 
None.gif
None.gif
using  System.Collections; 
None.gif
None.gif
using  System.Diagnostics; 
None.gif
None.gif
using  System.Management; 
None.gif
None.gif
using  System.Net; 
None.gif
None.gif
using  System.DirectoryServices; 
None.gif
None.gif
using  System.Runtime.InteropServices; 
None.gif
None.gif
using  System.Text.RegularExpressions; 
None.gif



获得本机的MAC地址 

None.gif         public   static   string  GetLocalMac() 
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
InBlock.gif
InBlock.gif            
string strMac = string.Empty; 
InBlock.gif
InBlock.gif            ManagementClass mc 
= new ManagementClass("Win32_NetworkAdapterConfiguration"); 
InBlock.gif
InBlock.gif            ManagementObjectCollection moc 
= mc.GetInstances(); 
InBlock.gif
InBlock.gif            
foreach(ManagementObject mo in moc) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif
InBlock.gif                
if ((bool)mo["IPEnabled"== true
InBlock.gif
InBlock.gif                    strMac 
+= mo["MacAddress"].ToString() ; 
InBlock.gif
InBlock.gif  
InBlock.gif
ExpandedSubBlockEnd.gif            }
 
InBlock.gif
InBlock.gif            
return strMac.ToUpper(); 
InBlock.gif
ExpandedBlockEnd.gif        }
 
None.gif
None.gif



获得远程计算机的MAC地址 

方法一:使用API,利用ARP协议,只能获得同网段计算机的MAC 

None.gif         [DllImport( " Iphlpapi.dll " )] 
None.gif
None.gif        
private   static   extern   int  SendARP(Int32 dest,Int32 host, ref  Int64 mac, ref  Int32 length); 
None.gif
None.gif        [DllImport(
" Ws2_32.dll " )] 
None.gif
None.gif        
private   static   extern  Int32 inet_addr( string  ip); 
None.gif
None.gif        
public   static   string  GetRemoteMac( string  clientIP) 
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
InBlock.gif
InBlock.gif            
string ip = clientIP; 
InBlock.gif
InBlock.gif            
if ( ip == "127.0.0.1"
InBlock.gif
InBlock.gif                ip 
= GetLocalIP()[0]; 
InBlock.gif
InBlock.gif            Int32 ldest
=inet_addr(ip); 
InBlock.gif
InBlock.gif            Int64 macinfo
=new Int64(); 
InBlock.gif
InBlock.gif            Int32 len
=6
InBlock.gif
InBlock.gif            
try 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif
InBlock.gif                SendARP(ldest,
0,ref macinfo,ref len); 
InBlock.gif
ExpandedSubBlockEnd.gif            }
 
InBlock.gif
InBlock.gif            
catch 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif
InBlock.gif                
return ""
InBlock.gif
ExpandedSubBlockEnd.gif            }
 
InBlock.gif
InBlock.gif            
string originalMACAddress = Convert.ToString(macinfo,16); 
InBlock.gif
InBlock.gif            
if (originalMACAddress.Length<12
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif
InBlock.gif                originalMACAddress 
= originalMACAddress.PadLeft(12,'0'); 
InBlock.gif
ExpandedSubBlockEnd.gif            }
 
InBlock.gif
InBlock.gif            
string macAddress; 
InBlock.gif
InBlock.gif            
if(originalMACAddress!="0000" && originalMACAddress.Length==12
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif
InBlock.gif                
string mac1,mac2,mac3,mac4,mac5,mac6; 
InBlock.gif
InBlock.gif                mac1
=originalMACAddress.Substring(10,2); 
InBlock.gif
InBlock.gif                mac2
=originalMACAddress.Substring(8,2); 
InBlock.gif
InBlock.gif                mac3
=originalMACAddress.Substring(6,2); 
InBlock.gif
InBlock.gif                mac4
=originalMACAddress.Substring(4,2); 
InBlock.gif
InBlock.gif                mac5
=originalMACAddress.Substring(2,2); 
InBlock.gif
InBlock.gif                mac6
=originalMACAddress.Substring(0,2); 
InBlock.gif
InBlock.gif                macAddress
=mac1+"-"+mac2+"-"+mac3+"-"+mac4+"-"+mac5+"-"+mac6; 
InBlock.gif
ExpandedSubBlockEnd.gif            }
 
InBlock.gif
InBlock.gif            
else 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif
InBlock.gif                macAddress
=""
InBlock.gif
ExpandedSubBlockEnd.gif            }
 
InBlock.gif
InBlock.gif            
return macAddress.ToUpper(); 
InBlock.gif
ExpandedBlockEnd.gif        }
 
None.gif
None.gif

方法二:使用windows的命令nbtstat 

None.gif         public   static   string  GetRemoteMacByNetBIOS( string  clientIP) 
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
InBlock.gif
InBlock.gif            
string ip = clientIP; 
InBlock.gif
InBlock.gif            
if ( ip == "127.0.0.1"
InBlock.gif
InBlock.gif                ip 
= GetLocalIP()[0]; 
InBlock.gif
InBlock.gif            
string dirResults=""
InBlock.gif
InBlock.gif            ProcessStartInfo psi  
= new ProcessStartInfo(); 
InBlock.gif
InBlock.gif            Process proc 
= new Process(); 
InBlock.gif
InBlock.gif            psi.FileName 
= "nbtstat.exe"
InBlock.gif
InBlock.gif            
//psi.RedirectStandardInput = false; 
InBlock.gif

InBlock.gif            psi.RedirectStandardOutput 
= true;psi.RedirectStandardError=true
InBlock.gif
InBlock.gif            psi.Arguments 
= "-A " + ip; 
InBlock.gif
InBlock.gif            psi.UseShellExecute 
= false
InBlock.gif
InBlock.gif            proc 
= Process.Start(psi); 
InBlock.gif
InBlock.gif            dirResults 
= proc.StandardOutput.ReadToEnd(); 
InBlock.gif
InBlock.gif            
string error = proc.StandardError.ReadToEnd(); 
InBlock.gif
InBlock.gif            proc.WaitForExit(); 
InBlock.gif
InBlock.gif            dirResults
=dirResults.Replace("\r","").Replace("\n","").Replace("\t",""); 
InBlock.gif
InBlock.gif  
InBlock.gif
InBlock.gif            Regex reg
=new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?((.)*?))__MAC",RegexOptions.IgnoreCase|RegexOptions.Compiled); 
InBlock.gif
InBlock.gif            Match mc
=reg.Match(dirResults+"__MAC"); 
InBlock.gif
InBlock.gif  
InBlock.gif
InBlock.gif            
if(mc.Success) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif
InBlock.gif                
return mc.Groups["key"].Value.ToUpper(); 
InBlock.gif
ExpandedSubBlockEnd.gif            }
 
InBlock.gif
InBlock.gif            
else 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                    
InBlock.gif
InBlock.gif               
return ""
InBlock.gif
ExpandedSubBlockEnd.gif            }
 
InBlock.gif
ExpandedBlockEnd.gif        }
 
None.gif

使用此方法需要足够的操作系统的权限。在Web中,可以将ASP.net用户加入管理员组。 

对于上面两个地方都用到的GetLocalIP是一个获取本机IP的方法: 

None.gif         public   static   string [] GetLocalIP() 
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
InBlock.gif
InBlock.gif            
string hostName = Dns.GetHostName(); 
InBlock.gif
InBlock.gif            IPHostEntry ipEntry
=Dns.GetHostByName(hostName); 
InBlock.gif
InBlock.gif            IPAddress[] arr
=ipEntry.AddressList; 
InBlock.gif
InBlock.gif            
string[] result = new string[arr.Length]; 
InBlock.gif
InBlock.gif            
for(int i=0;i 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif
InBlock.gif                result[i] 
= arr[i].ToString();   
InBlock.gif
ExpandedSubBlockEnd.gif            }
 
InBlock.gif
InBlock.gif            
return result; 
InBlock.gif
ExpandedBlockEnd.gif        }
 
None.gif

获得局域网内计算机的列表 

方法一:使用逐个IP地址扫描的方式

利用多线程来对每个IP逐个扫描。

None.gif ComputerAddressInfo cai  =   new  ComputerAddressInfo( " 192.168.1 " , 42 , 53 ); 
None.gif
None.gifThread thScan 
=   new  Thread( new  ThreadStart(cai.ScanComputers)); 
None.gif
None.gifthScan.Start(); 
None.gif
None.gif
// dot.gif 
None.gif

None.gif    
public   class  ComputerAddressInfo 
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
InBlock.gif
InBlock.gif        
private int startIP = 0
InBlock.gif
InBlock.gif        
private int endIP = 0
InBlock.gif
InBlock.gif        
private string ipPrefix = ""
InBlock.gif
InBlock.gif        
private ArrayList computerList = null
InBlock.gif
InBlock.gif   
InBlock.gif
InBlock.gif        
public ComputerAddressInfo(string ipPrefix,int startIP,int endIP) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif
InBlock.gif            
this.startIP = startIP; 
InBlock.gif
InBlock.gif            
this.endIP = endIP; 
InBlock.gif
InBlock.gif            
this.ipPrefix = ipPrefix; 
InBlock.gif
InBlock.gif            computerList 
= new ArrayList(); 
InBlock.gif
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif  
InBlock.gif        
public void ScanComputers() 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif
InBlock.gif            
for(int i=startIP;i<=endIP;i++
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif
InBlock.gif                
string scanIP = ipPrefix +"."+i.ToString(); 
InBlock.gif
InBlock.gif                IPAddress myScanIP 
= IPAddress.Parse(scanIP); 
InBlock.gif
InBlock.gif                IPHostEntry myScanHost 
= null
InBlock.gif
InBlock.gif                
string[] arr = new string[2]; 
InBlock.gif
InBlock.gif                
try 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif
InBlock.gif                    myScanHost 
= Dns.GetHostByAddress(myScanIP); 
InBlock.gif
ExpandedSubBlockEnd.gif                }
 
InBlock.gif
InBlock.gif                
catch 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif
InBlock.gif                    
continue
InBlock.gif
ExpandedSubBlockEnd.gif                }
 
InBlock.gif
InBlock.gif                
if (myScanHost != null
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif
InBlock.gif                    arr[
0= myScanHost.HostName; 
InBlock.gif
InBlock.gif                    arr[
1= scanIP; 
InBlock.gif
InBlock.gif                    computerList.Add(arr); 
InBlock.gif
ExpandedSubBlockEnd.gif                }
 
InBlock.gif
ExpandedSubBlockEnd.gif            }
 
InBlock.gif
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
ExpandedBlockEnd.gif    }
 
None.gif
None.gif 
None.gif

此方法速度比较慢。

方法二:使用Active Directory 

None.gif         public   static  ArrayList GetComputerList()  
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {  
InBlock.gif
InBlock.gif            ArrayList list 
= new ArrayList(); 
InBlock.gif
InBlock.gif            
//or  use  "WinNT://your_domain_name"  
InBlock.gif

InBlock.gif            DirectoryEntry  root  
=  new  DirectoryEntry("WinNT:"); 
InBlock.gif
InBlock.gif            DirectoryEntries  domains  
=  root.Children;  
InBlock.gif
InBlock.gif            domains.SchemaFilter.Add(
"domain");  
InBlock.gif
InBlock.gif            
foreach  (DirectoryEntry  domain  in  domains)  
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif
InBlock.gif                DirectoryEntries  computers  
=  domain.Children;  
InBlock.gif
InBlock.gif                computers.SchemaFilter.Add(
"computer");  
InBlock.gif
InBlock.gif                
foreach  (DirectoryEntry  computer  in  computers)  
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{  
InBlock.gif
InBlock.gif                    
object[] arr = new string[3]; 
InBlock.gif
InBlock.gif                    IPHostEntry  iphe 
= null
InBlock.gif
InBlock.gif                    
try 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif
InBlock.gif
InBlock.gif                        iphe  
=  Dns.GetHostByName(computer.Name); 
InBlock.gif
ExpandedSubBlockEnd.gif                    }
 
InBlock.gif
InBlock.gif                    
catch 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif
InBlock.gif
InBlock.gif                        
continue
InBlock.gif
ExpandedSubBlockEnd.gif                    }
 
InBlock.gif
InBlock.gif                    arr[
0= domain.Name; 
InBlock.gif
InBlock.gif                    arr[
1= computer.Name; 
InBlock.gif
InBlock.gif                    
if ( iphe != null && iphe.AddressList.Length >0 ) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif
InBlock.gif
InBlock.gif                        
for ( int i=0;i 
InBlock.gif
InBlock.gif                            arr[
2+= iphe.AddressList[i].ToString()+","
InBlock.gif
InBlock.gif                        arr[
2= arr[2].ToString().Remove(arr[2].ToString().Length-1,1); 
InBlock.gif
ExpandedSubBlockEnd.gif                    }
 
InBlock.gif
InBlock.gif                    
else 
InBlock.gif
InBlock.gif                        arr[
2= ""
InBlock.gif
InBlock.gif                    list.Add(arr); 
InBlock.gif
ExpandedSubBlockEnd.gif                }
  
InBlock.gif
ExpandedSubBlockEnd.gif            }
  
InBlock.gif
InBlock.gif            
return list; 
InBlock.gif
ExpandedBlockEnd.gif        }
  
None.gif

此方法速度也比较慢。

后记 

上面两个获得局域网内的计算机列表的方法都很费时,目前还没有找到更好的办法。

转载于:https://www.cnblogs.com/suchenge/articles/888795.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值