获得MAC地址的四个方法

添加人:iyond七级(4376分)   添加时间:2007-04-10    阅读次数:1311  收藏此教程

1.使用WMI查询表Win32_NetworkAdapterConfiguration即可获得。

2.使用ARP协议。请看这里

3.使用Windows命令nbtstat,也就是通过NetBIOS请看这里

4.查询SNMP(就是一种用于监视网络设备的协议)的MIB(管理信息数据库)。但这不是一件简单的事情,需要自己创建SNMP包,发送到交换机,然后对返回的响应进行解析。

下面是代碼:

using System;
using System.Diagnostics;
using System.Management;
using System.Net;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

namespace MACAddress
{
    
/// <summary>
    
/// MainClass 的摘要描述。
    
/// </summary>

    internal class MainClass
    
{
        
/// <summary>
        
/// 應用程式的主進入點。
        
/// </summary>

        [STAThread]
        
private static void Main(string[] args)
        
{
            GetMACByWMI();
            IPAddress[] ips 
= GetLocalIP();
            
foreach (IPAddress ip in ips)
            
{
                Console.WriteLine(GetMacByARP(ip.ToString()));
                
string mac = GetRemoteMacByNetBIOS(ip.ToString());
                
if ( mac.Length != 0 )
                    Console.WriteLine(mac);
                
else
                    Console.WriteLine(
"Fail to get MACAddress by NetBIOS");
                GetMACBySNMP(ip.ToString(),
"yourGroupName@yourVlanNumber");
            }

            Console.ReadLine();
        }


        
By WMI

        
By ARP

        
By NetBIOS

        
By SNMP
    }

}


SNMP Class


using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace MACAddress
{
    
/**//// <summary>
    
/// SNMP 的摘要描述。
    
/// </summary>

    public class SNMP
    
{
        
public SNMP()
        
{
        }


        
public byte[] Get(string request, string host, string community, string mibString)
        
{
            
byte[] packet = new byte[1024];
            
byte[] mib = new byte[1024];
            
int snmpLen;
            
int comLen = community.Length;
            
string[] mibVals = mibString.Split('.');
            
int mibLen = mibVals.Length;
            
int cnt = 0;
            
int temp;
            
int orgmibLen = mibLen;
            
int pos = 0;
            
for (int i = 0; i < orgmibLen; i++)
            
{
                temp 
= Convert.ToInt16(mibVals[i]);
                
if (temp > 127)
                
{
                    mib[cnt] 
= Convert.ToByte(128 + (temp / 128));
                    mib[cnt 
+ 1= Convert.ToByte(temp - ((temp / 128* 128));
                    cnt 
+= 2;
                    mibLen
++;
                }

                
else
                
{
                    mib[cnt] 
= Convert.ToByte(temp);
                    cnt
++;
                }

            }


            snmpLen 
= 29 + comLen + mibLen - 1;
            packet[pos
++= 0x30;
            packet[pos
++= Convert.ToByte(snmpLen - 2);

            packet[pos
++= 0x02;
            packet[pos
++= 0x01;
            packet[pos
++= 0x00;

            packet[pos
++= 0x04;
            packet[pos
++= Convert.ToByte(comLen);
            
byte[] data = Encoding.ASCII.GetBytes(community);
            
for (int i = 0; i < data.Length; i++)
            
{
                packet[pos
++= data[i];
            }


            
if (request == "get")
            
{
                packet[pos
++= 0xA0;
            }

            
else
            
{
                packet[pos
++= 0xA1;
            }

            packet[pos
++= Convert.ToByte(20 + mibLen - 1);

            packet[pos
++= 0x02;
            packet[pos
++= 0x04;
            packet[pos
++= 0x00;
            packet[pos
++= 0x00;
            packet[pos
++= 0x00;
            packet[pos
++= 0x01;

            packet[pos
++= 0x02;
            packet[pos
++= 0x01;
            packet[pos
++= 0x00;

            packet[pos
++= 0x02;
            packet[pos
++= 0x01;
            packet[pos
++= 0x00;

            packet[pos
++= 0x30;

            packet[pos
++= Convert.ToByte(6 + mibLen - 1);
            packet[pos
++= 0x30;
            packet[pos
++= Convert.ToByte(6 + mibLen - 1 - 2);
            packet[pos
++= 0x06;
            packet[pos
++= Convert.ToByte(mibLen - 1);

            packet[pos
++= 0x2b;
            
for (int i = 2; i < mibLen; i++)
            
{
                packet[pos
++= Convert.ToByte(mib[i]);
            }

            packet[pos
++= 0x05;