Results 1 to 12 of 12

Thread: [2005] MAC Address -> IP Address

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    65

    [2005] MAC Address -> IP Address

    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?

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] MAC Address -> IP Address

    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?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    65

    Re: [2005] MAC Address -> IP 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.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [2005] MAC Address -> IP Address

    I'm not so sure if VB can do it
    but I know you must send an "ARP request" to retrieve the MAC address
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] MAC Address -> IP Address

    You can do it Good ol' WMI, using the Win32_NetworkAdapterConfiguration WMI class. Below is an example.
    VB Code:
    1. **NOTE - add a reference to "System.Management" in your project
    2.         Dim MyobjectQuery As New System.Management.ObjectQuery("select * from Win32_NetworkAdapterConfiguration")
    3.         Dim MySearcher As New System.Management.ManagementObjectSearcher(MyobjectQuery)
    4.         For Each Mgmt As System.Management.ManagementObject In MySearcher.Get()
    5.             MessageBox.Show(Mgmt.GetText(TextFormat.Mof))
    6.         Next
    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.

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] MAC Address -> IP Address

    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:
    1. '***NOTE - once again, you need a reference to "System.Management" in order to use
    2.         Dim MyobjectQuery As New System.Management.ObjectQuery("select * from Win32_NetworkAdapterConfiguration")
    3.         Dim MySearcher As New System.Management.ManagementObjectSearcher(MyobjectQuery)
    4.         For Each Mgmt As System.Management.ManagementObject In MySearcher.Get()
    5.             'try/catch block needed because not all adapters will return these properties
    6.             Try
    7.                 MessageBox.Show(Mgmt("Caption").ToString)
    8.                 MessageBox.Show(Mgmt("MACAddress").ToString)
    9.                 'IP address property seems to be an array, so we have to cast it into one
    10.                 Dim MyArray As System.Array = DirectCast(Mgmt("IPAddress"), System.Array)
    11.                 For Each str As String In MyArray
    12.                     MessageBox.Show(str)
    13.                 Next
    14.             Catch
    15.             End Try
    16.         Next
    Last edited by gigemboy; Apr 7th, 2006 at 11:46 AM.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    65

    Re: [2005] MAC Address -> IP Address

    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.

  8. #8
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] MAC Address -> IP 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..

  9. #9
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2005] MAC Address -> IP Address

    gigemboy, you can have multiple network adapters on 1 computer which is probably why the IP addresses are an array.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  10. #10
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] MAC Address -> IP Address

    Quote Originally Posted by kasracer
    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?

  11. #11
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2005] MAC Address -> IP Address

    Quote Originally Posted by gigemboy
    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?
    The loopback address is implimented in the device's drivers. It shouldn't count that as a second IP.

    It's probably related to network cards like this one: http://www.newegg.com/Product/Produc...82E16833106217
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  12. #12
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: [2005] MAC Address -> IP Address

    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:
    1. [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
    2.     [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
    3.  
    4.     [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
    5.         [COLOR=Green]'/// buffer to receive the MacAddress into...[/COLOR]
    6.         [COLOR=Blue]Dim[/COLOR] buf(6) [COLOR=Blue]As Byte[/COLOR]
    7.         [COLOR=Green]'/// specify your ip to check here *** remember to add Imports System.NET at the top of the code window *** ...[/COLOR]
    8.         [COLOR=Blue]Dim[/COLOR] ip [COLOR=Blue]As[/COLOR] IPAddress = IPAddress.Parse("192.168.0.4")
    9.  
    10.         [COLOR=Blue]If[/COLOR] SendARP(BitConverter.ToInt32(IPAddress.Parse("192.168.0.4").GetAddressBytes, 0), 0, buf, 6) = 0 [COLOR=Blue]Then[/COLOR]
    11.             Console.WriteLine(BitConverter.ToString(buf, 0, 6))
    12.         [COLOR=Blue]End If[/COLOR]
    13.  
    14.     [COLOR=Blue]End Sub[/COLOR]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width