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.
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
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