Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save christopher-hopper/9062118 to your computer and use it in GitHub Desktop.
Save christopher-hopper/9062118 to your computer and use it in GitHub Desktop.
Find Windows processes listening on a port

Windows processes listening on a port

These examples assume you are using Windows 7 and have Cygwin installed with the awk package.

This single command will identify the process listening on port "2222" by feeding netstat output into tasklist using gawk.

netstat -aon | gawk ' $2~/:2222/ { system("tasklist /svc /FI \"PID eq " $5 "\"") } '
Image Name                     PID Services
========================= ======== ============================================
VBoxHeadless.exe             11148 N/A

For an explaination of what each command does and how this works, read on below.

Find the listening port

Use netstat to find the process id (PID) listening on a port.

In the code below, we feed the output through gawk to filter processes listening on port 2222 (column 2) and only output the Process id (column 5).

netstat -aon | gawk ' $2~/:2222/ { print $5 } '
11148

Identify the process

Use tasklist to identify the name of the PID you found with netstat.

tasklist /svc /FI "PID eq 11148"
Image Name                     PID Services
========================= ======== ============================================
VBoxHeadless.exe             11148 N/A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment