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
console.readline();
}by wmi#region by wmi
public
static
void getmacbywmi()
", mac);}}
#endregion
by arp#region by arp
[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);
public
static
string getmacbyarp(string clientip)
catch
string originalmacaddress = convert.tostring(macinfo, 16);
if (originalmacaddress.length < 12)
string macaddress;
if (originalmacaddress != "0000" && originalmacaddress.length == 12)
else
return macaddress.toupper();
}public
static ipaddress getlocalip()
#endregion
by netbios#region by netbios
public
static
string getremotemacbynetbios(string clientip)
address[ ]=[ ](?((.)*?))__mac", regexoptions.ignorecase | regexoptions.compiled);
match mc = reg.match(dirresults + "__mac");
if (mc.success)
else
} #endregion
by snmp#region by snmp
public
static
void getmacbysnmp(string ip,string vlan)
console.writeline("=",nextmib,value);}}
#endregion }}
snmp class
using system;
using system.net;
using system.net.sockets;
using system.text;
namespace macaddress
public
byte get(string request, string host, string community, string mibstring)
else
}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++)
if (request == "get")
else
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++] = 0x05;
packet[pos++] = 0x00;
socket sock = new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp);
sock.setsocketoption(socketoptionlevel.socket, socketoptionname.receivetimeout, 5000);
iphostentry ihe = dns.resolve(host);
ipendpoint iep = new ipendpoint(ihe.addresslist[0], 161);
endpoint ep = (endpoint) iep;
sock.sendto(packet, snmplen, socketflags.none, iep);
trycatch (socketexception)
return packet;
}public
string getnextmib(byte mibin)
output += "." + mibvalue;
}return output;}}
}
獲得mac位址的四個方法
using system using system.diagnostics using system.management using system.net using system.runtime.interopservices using system.text.regularexpressio...
Mapper的四個方法
1 protected void setup context context 一般用來載入一些初始化的工作,每個job執行一次 protected void setup context context throws ioexception,interruptedexception catch ioe...
FIle的四個構造方法
file file parent,string child 根據 parent 抽象路徑名和 child 路徑名字串建立乙個新file例項。file string pathname 通過將給定路徑名字串轉換為抽象路徑名來建立乙個新file例項。file string parent,string ch...