Search code examples
regexwindowsfindstrnetstat

Using Regex with netstat (Windows)


I am trying to get a single command line to display both TCP listening and established states and UDP connections.

So this would be one command line to show the following:

netstat -abfn  | findstr /r "[0-9]*[ED|ING]$"
netstat -abfn  | findstr UDP

I tried the below command line but am not able to sanitize the *

netstat -abfn  | findstr /r "[0-9]*[ED|ING|*:*]$"

Can anyone please post and explain the successful regex ?


Solution

  • The command you need is:

    netstat -abfn  | findstr /r "TCP.*[ING]$ TCP.*[ED]$ UDP.*$"
    

    That's because the findstr has limited use of regex's and doesn't support the | (OR) operator, but you can pass to it multiple patterns. In your case one for each:

    • TCP LISTENING TCP.*[ING]$
    • TCP ESTABLISHED TCP.*[ED]$
    • UDP UDP.*$

    Also, this regex is not case sensitive to make it so you must use the /i parameter, if you like.