Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2587
  • Last Modified:

Netstat with PID using VB.net

Found this code on Experts Exchange, but can't get it to work.
When the code hits the process.Start, another copy of my application opens, and netstat doesn't execute. Everything I try again ( using command button) another copy of my app loads.
Something silly happening here. but what?


Dim process As New Process()
 process.StartInfo.FileName = "netstat.exe"
  If includeProcess Then
            process.StartInfo.Arguments = "-b"
   End If
    process.StartInfo.CreateNoWindow = True
    process.StartInfo.ErrorDialog = False
    process.StartInfo.RedirectStandardOutput = True
    process.StartInfo.UseShellExecute = False
    process.Start()
Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text.RegularExpressions
 
Public Class ActiveConnection
    Public Protocol As String = ""
    Public LocalAddress As String = ""
    Public ForeignAddress As String = ""
    Public State As String = ""
    Public ProcessID As Integer
    Public ProcessExecutable As String = ""
End Class
 
Public Class NetStat
 
    Public Shared Function GetActiveConnections(ByVal includeProcess As Boolean) As List(Of ActiveConnection)
 
        Dim process As New Process()
 
        process.StartInfo.FileName = "netstat.exe"
 
        If includeProcess Then
            process.StartInfo.Arguments = "-b"
        End If
 
        process.StartInfo.CreateNoWindow = True
        process.StartInfo.ErrorDialog = False
        process.StartInfo.RedirectStandardOutput = True
        process.StartInfo.UseShellExecute = False
 
        process.Start()
 
        process.WaitForExit(5000)
 
        Dim output As String = process.StandardOutput.ReadToEnd()
 
        Return ParseConnections(output)
 
    End Function
 
    Private Shared Function ParseConnections(ByVal input As String) As List(Of ActiveConnection)
 
        Dim list As New List(Of ActiveConnection)
 
        Dim allLines() As String = input.Replace(vbCr, "").Split(Chr(10))
 
        Dim connection As ActiveConnection = Nothing
        For Each line As String In allLines
 
            If line.Trim().Length > 0 Then
                If ParseActiveConnection(line.Trim(), connection) Then
                    list.Add(connection)
                End If
            End If
 
        Next line
 
        Return list
 
    End Function
 
    Private Shared Function ParseActiveConnection(ByVal input As String, ByRef connection As ActiveConnection) As Boolean
 
        Dim patternSpaces As String = "(\s+)"
        Dim patternProtocol As String = "(?<proto>[A-Z\d]+)"
        Dim patternLocal As String = "(?<local>[A-Za-z\d:_.]+)"
        Dim patternForeign As String = "(?<foreign>[A-Za-z\d:_.]+)"
        Dim patternState As String = "(?<state>[A-Z_\d]+)"
        Dim patternPid As String = "?(?<pid>\d+)?"
 
        Dim pattern As String = patternProtocol & patternSpaces & _
         patternLocal & patternSpaces & patternForeign & _
         patternSpaces & patternState & patternSpaces & patternPid
 
        Dim addConnection As Boolean = False
 
        Dim matchConnection As Match = Regex.Match(input, pattern)
 
        If matchConnection.Success Then
            connection = New ActiveConnection
 
            connection.Protocol = matchConnection.Groups("proto").Value
            connection.LocalAddress = matchConnection.Groups("local").Value
            connection.ForeignAddress = matchConnection.Groups("foreign").Value
            connection.State = matchConnection.Groups("state").Value
            connection.ProcessID = CInt(matchConnection.Groups("pid").Value)
 
            addConnection = True
 
        Else
            Dim patternExe As String = "\[(?<exe>[A-Za-z\d.]+)\]"
            Dim matchExe As Match = Regex.Match(input, patternExe)
 
            If matchExe.Success Then
                connection.ProcessExecutable = matchExe.Groups("exe").Value
            End If
        End If
 
        Return addConnection
 
    End Function
 
End Class

Toggle HighlightingOpen in New WindowSelect All

Open in new window

0
Nick Price
Asked:
Nick Price
  • 4
  • 2
2 Solutions
 
käµfm³d 👽Commented:
Hmmm...  your code seems to work fine form me as is. What does the code that executes the code above look like (i.e. who calls this)?
0
 
Nick PriceTechnical ManagerAuthor Commented:

I've tried then process start on it's own, without the GetActiveConnections, and it does the same.
I've unable to get the netstat to kick off, and my application just opens another instance of itself.
This is how i'm calling it. The try doesn't fail.

Cheers Nick

    Try
            Dim Xcoll = NetStat.GetActiveConnections(False)
        Catch ex As Exception

        End Try

0
 
Nick PriceTechnical ManagerAuthor Commented:
I've just replaced the "netstat" with "notepad" and it opens notepad fine, and doesn't open another instance of my app. Something to do with netstat.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
käµfm³d 👽Commented:
Peculiar behavior indeed. The only other thing I can think to ask is: would it be plausible to post the full caller code?
0
 
Nick PriceTechnical ManagerAuthor Commented:
I've created a new project, and it is working now.  I have no clue why.

Regards

Nick



Dim process As New Process()

        process.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System)
        process.StartInfo.FileName = "netstat"
        process.StartInfo.Arguments = "-a -no"
        process.StartInfo.CreateNoWindow = True
        process.StartInfo.ErrorDialog = False
        process.StartInfo.RedirectStandardOutput = True
        process.StartInfo.UseShellExecute = False
        process.Start()
        process.WaitForExit(5000)
        Dim output As String = process.StandardOutput.ReadToEnd()
        MsgBox(output)

Open in new window

0
 
Nick PriceTechnical ManagerAuthor Commented:
Awarded some points to kaufmed for help and support, even through no solutions was required in the end.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now