我正在尝试重定向过程的standard
和error
输出(用VB.NET编写(,在此我正在执行连续命令。这是一个TSHARK命令(Wireshark的命令行工具(,可在运行时捕获网络流量。我尝试了以下两个命令:
-
-i 5 -B 1 -w /sample.pcap --print -Tfields -e frame.number -e ip.addr -e tcp -e _ws.col.Info -E separator=/t
-
-i 10 -T fields -e dns.qry.name src port 53
两个命令在命令提示中都非常有效。但是,当试图重定向代码中的输出时,只有命令编号1在执行StreamReader.ReadLine
时持续第二个命令。
请注意,我知道ReadLine
等待通过流读取一条新线路,其中两个命令都为每个捕获的数据包生成新的输出线。我也尝试使用Read
和ReadBlock
(关于代码所需的更改(,但没有用于第二个命令。
这是我的代码:
Public Class Form1
Dim output As String
Dim oProcess As New Process()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim oStartInfo As New ProcessStartInfo("C:Program FilesWiresharktshark.exe", "-i 10 -T fields -e dns.qry.name src port 53")
oStartInfo.UseShellExecute = False
oStartInfo.RedirectStandardOutput = True
oStartInfo.RedirectStandardError = True
oStartInfo.CreateNoWindow = True
oStartInfo.WindowStyle = ProcessWindowStyle.Hidden
oProcess.StartInfo = oStartInfo
Catch ex As Exception
MsgBox(ex)
End Try
BackgroundWorker1.RunWorkerAsync()
Button1.Enabled = False
Button2.Enabled = True
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
Threading.Thread.Sleep(2000)
If oProcess.Start() Then
Dim sOutput As String
Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
sOutput = oStreamReader.ReadLine
While Not sOutput Is Nothing
output = sOutput & vbNewLine
BackgroundWorker1.ReportProgress(10)
sOutput = sOutput + vbNewLine + oStreamReader.ReadLine
End While
End Using
Using oStreamReader As System.IO.StreamReader = oProcess.StandardError
sOutput = oStreamReader.ReadLine
While Not sOutput Is Nothing
output = sOutput & vbNewLine
BackgroundWorker1.ReportProgress(10)
sOutput = sOutput + vbNewLine + oStreamReader.ReadLine
End While
End Using
Else
MsgBox("Error starting the process")
End If
Catch ex As Exception
MsgBox(ex)
End Try
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
TextBox1.Text = output
TextBox1.Select(0, 0)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
BackgroundWorker1.CancelAsync()
Button1.Enabled = True
Button2.Enabled = False
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
这都是tshark
问题,从来没有VB.NET问题。如Mr.Kurt Knochner
所述,在回答问题时如何实时输出tshark输出?:
tshark输出被缓冲。如果需要,请使用
tshark
选项-L tshark在每个数据包后冲洗stdout。
并参考tshark
的文档:
-l打印每个数据包的信息后,汇总标准输出。
因此,为了使其正常工作,我将--print
选项以及-l
一起添加到我的命令行,现在它像魅力一样工作,现在看起来像:
tshark --print -l -i 10 -w ./sample.pcap -E separator=/t -T fields -e frame.number -e dns.qry.name src port 53
这是我的最终版本:
Public Class Form1
Dim outputQueue As New Queue(Of String)
Dim captureAdapterID As Integer = 0
Dim oProcess As Process
Private Sub Button1_Click(sender1 As Object, e1 As EventArgs) Handles Button1.Click
Button1.Enabled = False
Button2.Enabled = True
captureAdapterID = (ComboBox1.SelectedIndex + 1)
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender1 As Object, e1 As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
oProcess = New Process()
Dim oStartInfo As New ProcessStartInfo("C:Program FilesWiresharktshark.exe", " --print -l -i " & captureAdapterID & " -w ./sample.pcap -E separator=/t -T fields -e frame.number -e dns.qry.name src port 53")
oStartInfo.WorkingDirectory = New Uri(System.Windows.Forms.Application.StartupPath).LocalPath
oStartInfo.UseShellExecute = False
oStartInfo.RedirectStandardOutput = True
oStartInfo.RedirectStandardError = True
oStartInfo.CreateNoWindow = True
oStartInfo.WindowStyle = ProcessWindowStyle.Hidden
oProcess.StartInfo = oStartInfo
If oProcess.Start() Then
appendOutput("Capturing on device: " & captureAdapterID & " started.")
Dim sOutput As String
Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
sOutput = oStreamReader.ReadLine
While Not sOutput Is Nothing
appendOutput(sOutput)
sOutput = oStreamReader.ReadLine
End While
End Using
Using oStreamReader As System.IO.StreamReader = oProcess.StandardError
sOutput = oStreamReader.ReadLine
While Not sOutput Is Nothing
appendOutput(sOutput)
sOutput = oStreamReader.ReadLine
End While
End Using
MsgBox("finished")
Else
MsgBox("Error starting the process")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
BackgroundWorker1.ReportProgress(10)
End Try
End Sub
Private Sub appendOutput(sOutput As String)
outputQueue.Enqueue(sOutput)
BackgroundWorker1.ReportProgress(10)
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Dim i As Integer = 0
For i = 0 To outputQueue.Count - 1 Step 1
RichTextBox1.AppendText(outputQueue.Dequeue & vbNewLine)
Next
RichTextBox1.ScrollToCaret()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
BackgroundWorker1.CancelAsync()
oProcess.Kill()
Button1.Enabled = True
Button2.Enabled = False
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim process As New Process()
Dim oStartInfo As New ProcessStartInfo("C:Program FilesWiresharktshark.exe", " -D")
oStartInfo.WorkingDirectory = New Uri(System.Windows.Forms.Application.StartupPath).LocalPath
oStartInfo.UseShellExecute = False
oStartInfo.RedirectStandardOutput = True
oStartInfo.RedirectStandardError = True
oStartInfo.CreateNoWindow = True
oStartInfo.WindowStyle = ProcessWindowStyle.Hidden
process.StartInfo = oStartInfo
If process.Start() Then
Dim sOutput As String
Using oStreamReader As System.IO.StreamReader = process.StandardOutput
sOutput = oStreamReader.ReadToEnd
If Not sOutput Is Nothing Then
ComboBox1.Items.AddRange(sOutput.Trim.Split(vbNewLine))
Try
ComboBox1.SelectedIndex = 0
Catch ex As Exception
End Try
End If
End Using
Else
MsgBox("Error starting the get adapter process failed")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class