其实你只要搜索一下 MAC 就会查到很多以前的贴子,不用浪费这100分的:[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest,Int32 host,byte[] mac,ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
public static string GetMACByIP(string ip)
{
try
{
byte[] aa=new byte[6];Int32 ldest= inet_addr(ip); //目的地的ip Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest,0, aa, ref len); return BitConverter.ToString( aa, 0, 6 );;
}
catch(Exception err)
{
throw err;
}
}
private static extern int SendARP(Int32 dest,Int32 host,byte[] mac,ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
public static string GetMACByIP(string ip)
{
try
{
byte[] aa=new byte[6];Int32 ldest= inet_addr(ip); //目的地的ip Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest,0, aa, ref len); return BitConverter.ToString( aa, 0, 6 );;
}
catch(Exception err)
{
throw err;
}
}
解决方案 »
- 未将对象引用设置到对象的实例。
- backgroundWorker取消后,重新开始就报错:此 BackgroundWorker 当前正忙,无法同时运行多个任务。
- 自定义消息接收问题
- 如何插入记录?
- 很菜的菜鸟问!
- 请问如何把dbgrid中某一列链接的下化线不显示,谢谢
- dataSet 运算问题
- 刚学C#。如何引用程序集,求助!求助!
- Repeater里面能否批量生成服务器控件?id ='<%# DataBinder.Eval(Container.DataItem,"ss"这种类型的
- 如何使用DevExpress GridControl控件代替传统的DataGridView实现表头全选的功能
- datalist不能现实jpeg图像。只能显示gif图象。
- 突然发生的奇怪的问题:“创建调试信息文件“F:\CRM\obj\Debug\CRM.pdb”时发生意外的错误 --“F:\CRM\obj\Debug\CRM.pdb: 进程无法访问
{
string dirResults="";
ProcessStartInfo psi = new ProcessStartInfo();
Process proc = new Process();
psi.FileName = "nbtstat";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = "-A " + IP;
psi.UseShellExecute = false;
proc = Process.Start(psi);
dirResults = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
dirResults=dirResults.Replace("\r","").Replace("\n","").Replace("\t","");Regex reg=new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?<key>((.)*?))__MAC",RegexOptions.IgnoreCase|RegexOptions.Compiled);
Match mc=reg.Match(dirResults+"__MAC");if(mc.Success)
{
return mc.Groups["key"].Value;
}
else
{
reg=new Regex("Host not found",RegexOptions.IgnoreCase|RegexOptions.Compiled);
mc=reg.Match(dirResults);
if(mc.Success)
{
return "Host not found!";
}
else
{
return "";
}
}
}
[DllImport("Iphlpapi.dll")]
private static unsafe extern int SendARP(Int32 dest,Int32 host,ref IntPtr mac,ref IntPtr length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);Int32 ldest= inet_addr("157.60.68.163");//目的地的ip
Int32 lhost= inet_addr("157.60.68.33");//本地的iptry
{
Byte[] macinfo=new Byte[6];
Int32 length=6;IntPtr mac=new IntPtr(macinfo[0]);
IntPtr len=new IntPtr(6);
int ii=SendARP(ldest,lhost, ref mac, ref len);Console.WriteLine("Mac Add:"+mac);
Console.WriteLine("length:"+len);
}
catch(Exception err)
{
Console.WriteLine(err);
}
//新建连接
string username="computerusername"; //远程计算机的用户名
string userpass="computerpass"; //远程计算机的密码
string computer="computer"; //你要访问的远程计算机名也可以是ip地址
ConnectionOptions con=new ConnectionOptions();
con.Username=username;
con.Password=userpass;
ManagementScope ms=new ManagementScope("\\\\"+computer+"\\root\\cimv2",con);
try
{
ms.Connect();
}
catch
{
MessageBox.Show("远程计算机不可用");
}
ManagementClass mc=new ManagementClass(ms,new ManagementPath("Win32_NetworkAdapterConfiguration"),null);ManagementObjectCollection mo=mc.GetInstances();foreach(ManagementObject o in mo)
{
Console.WriteLine(o["MACAddress"].ToString()); //这就是你想要的
}
<%
strIP = Request.ServerVariables("REMOTE_ADDR")
strMac = GetMACAddress(strIP)
strHost = Request.ServerVariables("REMOTE_HOST")
Function GetMACAddress(strIP)
Set net = Server.CreateObject("wscript.network")
Set sh = Server.CreateObject("wscript.shell")
sh.run "%comspec% /c nbtstat -A " & strIP & " > c:\" & strIP & ".txt",0,true
Set sh = nothing
Set fso = createobject("scripting.filesystemobject")
Set ts = fso.opentextfile("c:\" & strIP & ".txt")
macaddress = null
Do While Not ts.AtEndOfStream
data = ucase(trim(ts.readline))
If instr(data,"MAC ADDRESS") Then
macaddress = trim(split(data,"=")(1))
Exit Do
End If
loop
ts.close
Set ts = nothing
'fso.deletefile "c:\" & strIP & ".txt"
Set fso = nothing
GetMACAddress = macaddress
End Function
response.write(macaddress)
%>