Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a built-in function in ASP.NET to resolve a MAC address from an IP address
    text
    copied!<p>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.</p> <p>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.</p> <hr> <p>Okay, for reference, this is what I did:</p> <pre><code>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 " &amp; 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 " &amp; ex.Message End Try End Function </code></pre> <p>I then called it with </p> <pre><code>get_MAC(Request.UserHostAddress()) </code></pre> <p>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).</p> <p>But back to the main question: Is there any other way to do it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload