Vb.net 解析arp.exe-WaitForExit(60000)超时
我正在尝试解析arp.exe的输出,以获取计算机的MAC地址。我能够解析ping.exe并获取IP地址,但由于某些原因,arp.exe似乎永远不会退出。如果我删除这些参数,它几乎会立即退出。如果我自己在命令提示符下运行命令,它将在不到一秒钟的时间内输出结果。我不知道为什么它不起作用Vb.net 解析arp.exe-WaitForExit(60000)超时,vb.net,Vb.net,我正在尝试解析arp.exe的输出,以获取计算机的MAC地址。我能够解析ping.exe并获取IP地址,但由于某些原因,arp.exe似乎永远不会退出。如果我删除这些参数,它几乎会立即退出。如果我自己在命令提示符下运行命令,它将在不到一秒钟的时间内输出结果。我不知道为什么它不起作用 Public Function DNSLookup(ByVal dnsName As String) #If Not Debug Then Try #End If Dim p As New Proc
Public Function DNSLookup(ByVal dnsName As String)
#If Not Debug Then
Try
#End If
Dim p As New Process
With p.StartInfo
.FileName = "ping.exe"
.Arguments = "-n 1 -4 " + dnsName 'Send 1 echo/packet (-n 1) and force IPv4 (-4)
.CreateNoWindow = True
.RedirectStandardOutput = True
.RedirectStandardError = True
.UseShellExecute = False
End With
p.Start()
If p.WaitForExit(5000) Then
'Find and parse "Reply from xxx.xxx.xxx.xxx: bytes=xx time=xms TTL=xxx"
Dim result As String = p.StandardOutput.ReadToEnd 'Read the result from the command line
Dim i As Integer = result.IndexOf("Reply from ") + 11 '11 is the length of "Reply from "
result = result.Substring(i, result.IndexOf(": bytes=") - i) 'Get the IP from the command line output
Return result
Else
Throw New System.Exception("DNS lookup failed. Timeout exceeded")
End If
#If Not Debug Then
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
#End If
End Function
Public Function GetMACAddress(ByVal dnsName As String)
Dim ip As String = DNSLookup(dnsName)
#If Not Debug Then
Try
#End If
Dim arp As New Process
With arp.StartInfo
.FileName = "arp.exe"
.Arguments = "-a" ' | find " + Chr(34) + ip + Chr(34) 'Example: arp -a | find "192.168.0.1"
.CreateNoWindow = True
.RedirectStandardOutput = True
.RedirectStandardError = True
.RedirectStandardInput = False
.UseShellExecute = False
End With
arp.Start()
If arp.WaitForExit(60000) Then
MsgBox(arp.StandardOutput.ReadToEnd)
Else
Throw New System.Exception("ARP lookup failed. Timeout exceeded")
End If
Return 1 '### TO DO: Return parsed MAC address
#If Not Debug Then
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
#End If
End Function
我不知道为什么昨晚设置为-a的参数不起作用,就像我今天早上运行时一样。然而,我意识到我的完整参数不起作用的原因是| find不是arp.exe认可的参数。为了解决这个问题,我使用了cmd.exe/c arp。更新代码如下:
Public Function GetMACAddress(ByVal dnsName As String)
Dim ip As String = DNSLookup(dnsName)
#If Not Debug Then
Try
#End If
Dim arp As New Process
With arp.StartInfo
.FileName = "cmd.exe"
.Arguments = "/c arp -a | find " + Chr(34) + ip + Chr(34) 'Example: arp -a | find "192.168.0.1"
.CreateNoWindow = False
.RedirectStandardOutput = True
.RedirectStandardError = True
.RedirectStandardInput = False
.UseShellExecute = False
End With
arp.Start()
If arp.WaitForExit(5000) Then
MsgBox(arp.StandardOutput.ReadToEnd)
Else
Throw New System.Exception("ARP lookup failed. Timeout exceeded")
End If
Return 1 '### TO DO: Return parsed IP address
#If Not Debug Then
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
#End If
End Function
嗯。。。。在您的第一个示例中,“查找”部分被注释掉。。。这就是它不起作用的原因。你甚至可以看到它被涂成灰色,这立刻跳到了眼睛上……我评论了一下,看看这是否是问题的根源。但是由于一些未知的原因,当只使用-a而不使用其他东西时,它仍然不起作用。然而第二天早上,当我再次运行它时,没有做任何更改,仍然使用-a,只有它起作用了。。。我不明白为什么会发生这种情况:他把“查找”部件放回原处,然后马上又把它弄坏了。使用cmd/c可以很好地处理| find参数。最好实际调用WINAPI函数来执行此类任务,使用命令行工具和传递参数总是会产生意外的结果,这根本不是在windows中开发软件的方式,它只适用于linuxThank的链接。一开始我想使用API,但一开始很难找到关于它的任何信息。事实上,我最终找到了那个页面,不幸的是,就在意识到你已经为我发布了链接lol之前。我现在正在使用API进行ping和arp。另外:如果你想访问不可访问的WINAPI函数,请不要使用Interop,如果你不确切知道自己在做什么,Interop很容易出错——这需要深入的了解和知识。在您的解决方案中创建一个C++/CLR项目——您可以在VB或C中轻松安全地调用您的CLR方法;没有所有的互操作开销。