Search code examples
c#asp.netvb.netmac-addressarp

Is there a built-in function in ASP.NET to resolve a MAC address from an IP address


I'm about to start developing a simple internal web application to log visitors to my office. Basically, they will read an AUP, then enter their name to indicate acknowledgement. The application will also log the visitor's IP using Request.UserHostAddress(). In addition to logging the IP, I'd like to run a script to make a DHCP reservation with the IP I get from the application, but I need to have the MAC address as well. This application will only be used within one subnet, and I'm planning to use a small DHCP range for the visitors.

Is there any built-in function I can use to resolve the MAC address from the IP via ARP? I'd prefer not to have to use a workaround if there's an existing function.


Okay, for reference, this is what I did:

Private Function get_MAC(ByVal ip As String) As String
    Dim proc As New System.Diagnostics.Process
    Try
        proc.StartInfo.UseShellExecute = False
        proc.StartInfo.RedirectStandardOutput = True
        proc.StartInfo.CreateNoWindow = True
        proc.StartInfo.FileName = Environment.ExpandEnvironmentVariables("%WINDIR%\system32\ping.exe")
        proc.StartInfo.Arguments = ip
        proc.Start()
        proc.StartInfo.Arguments = "-a " & ip
        proc.StartInfo.FileName = Environment.ExpandEnvironmentVariables("%WINDIR%\system32\arp.exe")
        proc.Start()
        Dim output As String = proc.StandardOutput.ReadToEnd
        Dim rgx As New Regex("\w{2}-\w{2}-\w{2}-\w{2}-\w{2}-\w{2}")
        If rgx.IsMatch(output) Then
            Return rgx.Match(output).Value
        Else
            Return "ERROR No MAC address found."
        End If
    Catch ex As Exception
        Return "ERROR " & ex.Message
    End Try
End Function

I then called it with

get_MAC(Request.UserHostAddress())

Note that this only works because the web server is guaranteed to be in the same subnet as the host. Also if you decide to test this with Visual Web Developer, note that the test server will return UserHostAddress as ::1 (the IPv6 loopback address).

But back to the main question: Is there any other way to do it?


Solution

  • http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/3435b288-a48f-46cc-b247-0f4c3deb5f89/

    " How to retrieve IP and MAC address from DHCP Server using C# "