从IP获取MAC地址 [英] get MAC Address from IP
问题描述
我想从一台机器上获取mac地址,我的IP地址为
那台机器,我该怎么办?
我知道如何从以下
代码获取本地机器的mac地址:
Dim mc As System.Management。 ManagementClass
Dim mo As System.Management.ManagementObject
mc = New
System.Management.ManagementClass(" Win32_NetworkAd apterConfiguration")
Dim moc As System.Management.ManagementObjectCollection =
mc.GetInstances()
For each mo in moc
如果mo.Item(" IPEnabled")= True则
MsgBox(mo.Item(" MacAddress")。ToString())
结束If
下一页
谢谢!
Hi,
I want to get the mac address from a machine, which i have the IP address of
that machine, how can i do that?
I know how to get the mac address of the local machine from the following
code:
Dim mc As System.Management.ManagementClass
Dim mo As System.Management.ManagementObject
mc = New
System.Management.ManagementClass("Win32_NetworkAd apterConfiguration")
Dim moc As System.Management.ManagementObjectCollection =
mc.GetInstances()
For Each mo In moc
If mo.Item("IPEnabled") = True Then
MsgBox(mo.Item("MacAddress").ToString())
End If
Next
Thanks!
推荐答案
" YAN" < YA*@discussions.microsoft.com>在消息中写道
news:95 ********************************** @ microsof t.com ...
"YAN" <YA*@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
我想从一台机器上获取mac地址,我有这台机器的IP地址
我可以这样做吗?
Hi,
I want to get the mac address from a machine, which i have the IP address
of
that machine, how can i do that?
你不能。这两者之间没有任何关系。
此外,一台机器可能有多个IP地址和多个MAC
地址。
John Saunders
You can''t. There''s no relationship between the two.
And besides, a machine may have multiple IP addresses and multiple MAC
addresses.
John Saunders
您可能想尝试这段代码。但是,它没有完全测试(虽然在
本地网络中工作)。
[DllImport(" iphlpapi.dll",ExactSpelling = true)]
public static extern int SendARP(int DestIP,int SrcIP,[Out] byte []
pMacAddr,ref int PhyAddrLen);
IPAddress ip = IPAddress.Parse(" 10.0.x.x"); //实际IP
int rv;
string macStr;
byte [] mac = new byte [6];
int maclen = mac.Length;
rv = SendARP(BitConverter.ToInt32(ip.GetAddressBytes(),0),0,mac,ref
maclen);
if(rv == 0)//如果不是0,则错误
{
macStr = BitConverter.ToString( mac,0,6);
Console.WriteLine(macStr);
}
" YAN" < YA*@discussions.microsoft.com>在消息中写道
news:95 ********************************** @ microsof t.com ...
我想从机器上获取mac地址,我的IP地址为
那台机器,我怎么能这样做?
我知道如何从以下
代码获取本地机器的mac地址:
Dim mc As System.Management.ManagementClass
Dim mo As System.Management.ManagementObject
mc = New
System.Management .ManagementClass(" Win32_NetworkAd apterConfiguration")
Dim moc As System.Management.ManagementObjectCollection =
mc.GetInstances()
For Each mo In moc
如果mo.Item(" IPEnabled")= True则
MsgBox(mo.Item(" MacAddress")。ToString() )
结束如果
下一页
谢谢!
You might want to try this code. But, its not tested fully (works in the
local network though).
[DllImport("iphlpapi.dll", ExactSpelling=true)]
public static extern int SendARP( int DestIP, int SrcIP, [Out] byte[]
pMacAddr, ref int PhyAddrLen );
IPAddress ip = IPAddress.Parse("10.0.x.x"); // Actual IP
int rv;
string macStr;
byte[] mac = new byte[6];
int maclen = mac.Length;
rv = SendARP(BitConverter.ToInt32(ip.GetAddressBytes(), 0), 0, mac, ref
maclen);
if (rv == 0) // If not 0, error
{
macStr = BitConverter.ToString(mac, 0, 6);
Console.WriteLine (macStr);
}
"YAN" <YA*@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
Hi,
I want to get the mac address from a machine, which i have the IP address of
that machine, how can i do that?
I know how to get the mac address of the local machine from the following
code:
Dim mc As System.Management.ManagementClass
Dim mo As System.Management.ManagementObject
mc = New
System.Management.ManagementClass("Win32_NetworkAd apterConfiguration")
Dim moc As System.Management.ManagementObjectCollection =
mc.GetInstances()
For Each mo In moc
If mo.Item("IPEnabled") = True Then
MsgBox(mo.Item("MacAddress").ToString())
End If
Next
Thanks!
" John Saunders" < johnwsaundersiii at hotmail.com>在消息中写道
新闻:%2 ****************** @ TK2MSFTNGP09.phx.gbl ...
"John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
"颜" < YA*@discussions.microsoft.com>在消息中写道
新闻:95 ********************************** @ microsof t.com。 ..
"YAN" <YA*@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
我想从机器上获取mac地址,我有那台机器的IP地址
那台机器怎么办? ?
Hi,
I want to get the mac address from a machine, which i have the IP address
of
that machine, how can i do that?
你不能。这两者之间没有关系。
此外,一台机器可能有多个IP地址和多个MAC地址。
John Saunders >
MAC地址和IP地址之间没有关系?我读得对吗!
当然还有一种关系,你怎么会从第3层(IP)
转移到第2层(MAC)。
实际上这个转换有一个完整的协议。它被称为ARP。
基本上在该段上进行ARP广播,要求所有设备如果拥有所述IP,则使用其MAC来响应
。这是
过程的简化,但它基本上是发生了什么。
我不知道如何在.Net中这样做但是说没有关系是
显然不是这样的。
当然我可能误读了你的帖子,但它的所有好事实
无论如何。
干杯,
Gareth。
No relationship between MAC address and IP address? Did I read that right!
Of course there''s a relationship how else would you move from layer 3 (IP)
to layer 2 (MAC).
In fact there is a whole protocol for this conversion. Its called ARP.
Basically ARP broadcasts on the segment asking all the devices to repond
with their MAC if they own the stated IP. This is a simplification of the
process but it is basically what happens.
I have no idea how to do this in .Net but saying there is no relationship is
clearly not the case.
Of course I may have misread your post but its all good factual stuff
anyway.
Cheers,
Gareth.
这篇关于从IP获取MAC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!