Skip to content

Instantly share code, notes, and snippets.

@ashnur
Forked from thusoy/netst
Created October 2, 2017 10:10
Show Gist options
  • Save ashnur/a81362cb6bc82f233d0ff1f25caceadc to your computer and use it in GitHub Desktop.
Save ashnur/a81362cb6bc82f233d0ff1f25caceadc to your computer and use it in GitHub Desktop.
Windows equivalent of netstat -tulpn
#!/bin/bash
# Normally windows doesn't give out the process name when you run netstat -ano, thus making it
# hard to figure out what process is bound to the different ports. This script extracts the PID
# from the netstat output, and fetches the corresponding binary name from the tasklist, and
# merges the two together. Needs a MinGW environment like Git Bash and python to work.
{ tasklist | tail -n +5; echo ===; netstat -ano | grep LIST; } | python -c "
import sys
pad = [0, 21, 9, 0, 5, 8, 0]
lines = [line.strip() for line in sys.stdin]
sep = lines.index('===')
pids, listeners = lines[:sep], lines[sep+1:]
pid_map = {s.split()[1]: [s.split()[2], s.split()[0]] for s in pids}
list_map = {s.split()[4]: s.split() for s in listeners}
print('\n'.join(str(' '.join(val.ljust(pad[i]) for i, val in enumerate(v + pid_map[k]))) for k, v in list_map.items()))"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment