我即将开始开发一个简单的内部Web应用程序来将访问者记录到我的办公室。基本上,他们会读取AUP,然后输入他们的名字来表示确认。该应用程序还将使用Request.UserHostAddress()记录访问者的IP。除了记录IP之外,我还想运行一个脚本来使用从应用程序获得的IP进行DHCP预留,但我还需要有MAC地址。这个应用程序只能在一个子网内使用,而且我打算为访问者使用一个小的DHCP范围。在ASP.NET中是否有内置函数来解析IP地址的MAC地址
是否有任何内置函数可用于通过ARP解析IP地址的MAC地址?如果存在现有功能,我不希望使用解决方法。
好,以供参考,这是我做的:
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
然后,我
get_MAC(Request.UserHostAddress())
请注意,这只是工作,因为Web服务器是保证把它称为在与主机相同的子网中。另外,如果您决定使用Visual Web Developer进行测试,请注意测试服务器将返回UserHostAddress as :: 1(IPv6回送地址)。
但回到主要问题:有没有其他方法可以做到这一点?
您可以分析'ARP -a'命令的输出 “如何使用C#检索DHCP服务器的IP和MAC地址”? –
我怀疑asp.net会这样做,因为MAC很少适用于Web服务器。主要的.NET BCL可能或Windows命令行。 –
@ssg是的,我想这可能会有更好的解决方案。哦:( – Seyren