Is it possible to get an IP Address from just a MAC Address? I've found some information on ARP, but it doesn't seem to be what I'm looking for.
Any Ideas?
Printable View
Is it possible to get an IP Address from just a MAC Address? I've found some information on ARP, but it doesn't seem to be what I'm looking for.
Any Ideas?
MAC addresses are unique in that no other network card on the planet has the same one. IP addresses arent. They can change. So are you just wanting the present IP address that is associated with a particular MAC address?
Yes.
We have a bunch of machines on our network and I need to be able to get ahold of that machine again no matter what its IP or Machine Name is. Both the IP and Name change constantly on each machine.
I'm not so sure if VB can do it
but I know you must send an "ARP request" to retrieve the MAC address
You can do it :) Good ol' WMI, using the Win32_NetworkAdapterConfiguration WMI class. Below is an example.
What this will do is loop through all instances if network adapter configurations, right now it just lists all the properties for all of them. What you would do is look at each one to figure out what properties you want. One of them will have both the IP address and the MAC address of the interface.VB Code:
**NOTE - add a reference to "System.Management" in your project Dim MyobjectQuery As New System.Management.ObjectQuery("select * from Win32_NetworkAdapterConfiguration") Dim MySearcher As New System.Management.ManagementObjectSearcher(MyobjectQuery) For Each Mgmt As System.Management.ManagementObject In MySearcher.Get() MessageBox.Show(Mgmt.GetText(TextFormat.Mof)) Next
Once thing I noticed is that IP address seems to be an array property... why this is, I do not know, as I dont know how one network interface would have two IP's. You have to cast the property into an array, like the below example. The below example loops through all network interfaces, and displays the Name, Mac address (if there is one), and IP address (if there is one)...
VB Code:
'***NOTE - once again, you need a reference to "System.Management" in order to use Dim MyobjectQuery As New System.Management.ObjectQuery("select * from Win32_NetworkAdapterConfiguration") Dim MySearcher As New System.Management.ManagementObjectSearcher(MyobjectQuery) For Each Mgmt As System.Management.ManagementObject In MySearcher.Get() 'try/catch block needed because not all adapters will return these properties Try MessageBox.Show(Mgmt("Caption").ToString) MessageBox.Show(Mgmt("MACAddress").ToString) 'IP address property seems to be an array, so we have to cast it into one Dim MyArray As System.Array = DirectCast(Mgmt("IPAddress"), System.Array) For Each str As String In MyArray MessageBox.Show(str) Next Catch End Try Next
This gets the MAC Address for my local machine right? This code is very interesting, but I need to get the IP of a remote machine by its MAC Address.
Well there is a way to do it from another machine on the network by supplying a managementscope parameter of the ManagementObjectSearcher object (in codebank submission). If you are referring to machines outside the network, then I dont think this would apply..
gigemboy, you can have multiple network adapters on 1 computer which is probably why the IP addresses are an array.
Yeah but it loops for all network interfaces. Each network interface has the property that is an array. You wont have 2 IP's for 1 network interface... as far as I know... unless maybe 1 will return the regular IP and loopback IP?Quote:
Originally Posted by kasracer
The loopback address is implimented in the device's drivers. It shouldn't count that as a second IP.Quote:
Originally Posted by gigemboy
It's probably related to network cards like this one: http://www.newegg.com/Product/Produc...82E16833106217
you don't really need to add references to the ManagementObject, you could always use a simple API call ;) ( *** just as an alternative *** )
on your local network you can use the SendARP API, you can also use this for any network interfaces on your own machine.
here's a quick example i bunged together for you , it's based on a few C# examples floating about, i just modified it to work in VB.NET...
VB Code:
[COLOR=Blue]Private Declare[/COLOR] [COLOR=Blue]Function[/COLOR] inet_addr [COLOR=Blue]Lib[/COLOR] "ws2_32.dll" ([COLOR=Blue]ByVal[/COLOR] cp [COLOR=Blue]As String[/COLOR]) [COLOR=Blue]As[/COLOR] Int32 [COLOR=Blue]Private Declare Function[/COLOR] SendARP [COLOR=Blue]Lib[/COLOR] "IPHLPAPI.dll" ([COLOR=Blue]ByVal[/COLOR] DestIP [COLOR=Blue]As Integer[/COLOR], [COLOR=Blue]ByVal[/COLOR] SrcIP [COLOR=Blue]As[/COLOR] Int32, [COLOR=Blue]ByVal[/COLOR] pMacAddr [COLOR=Blue]As Byte[/COLOR](), [COLOR=Blue]ByRef [/COLOR] PhyAddrLen [COLOR=Blue]As[/COLOR] Int32) [COLOR=Blue]As[/COLOR] Int32 [COLOR=Blue]Private Sub[/COLOR] Button1_Click([COLOR=Blue]ByVal[/COLOR] sender [COLOR=Blue]As[/COLOR] System.Object, [COLOR=Blue]ByVal[/COLOR] e [COLOR=Blue]As[/COLOR] System.EventArgs) [COLOR=Blue]Handles[/COLOR] Button1.Click [COLOR=Green]'/// buffer to receive the MacAddress into...[/COLOR] [COLOR=Blue]Dim[/COLOR] buf(6) [COLOR=Blue]As Byte[/COLOR] [COLOR=Green]'/// specify your ip to check here *** remember to add Imports System.NET at the top of the code window *** ...[/COLOR] [COLOR=Blue]Dim[/COLOR] ip [COLOR=Blue]As[/COLOR] IPAddress = IPAddress.Parse("192.168.0.4") [COLOR=Blue]If[/COLOR] SendARP(BitConverter.ToInt32(IPAddress.Parse("192.168.0.4").GetAddressBytes, 0), 0, buf, 6) = 0 [COLOR=Blue]Then[/COLOR] Console.WriteLine(BitConverter.ToString(buf, 0, 6)) [COLOR=Blue]End If[/COLOR] [COLOR=Blue]End Sub[/COLOR]