Skip to content

Instantly share code, notes, and snippets.

@tkellogg
Last active January 19, 2024 21:23
Show Gist options
  • Save tkellogg/4916424cf0018aed3ad4710fe5d41fa8 to your computer and use it in GitHub Desktop.
Save tkellogg/4916424cf0018aed3ad4710fe5d41fa8 to your computer and use it in GitHub Desktop.
ports.sh — What servers am I running?

What?

Find all the servers I have running on my laptop. It just prints port, pid and the command that started it (from ps aux)

Why?

Why not?

No, Really...

idk i just have a lot of servers running on my laptop and i sometimes forget where/what each is

It works fine on Mac OS 14.2

#!/bin/zsh
for line in $(netstat -vanp tcp | awk '$6 == "LISTEN" && $1 == "tcp4" { system("echo " $4 "." $9 " | awk -F. \"{print \\$(NF-1) \\\".\\\" \\$NF}\"") }')
do
port=$(echo $line | awk -F. '{print $1}')
pid=$(echo $line | awk -F. '{print $2}')
prog=$(ps aux | awk '$2 == "'$pid'" { $1=$2=$3=$4=$5=$6=$7=$8=$9=$10=""; print $0 }')
echo "localhost:$port\t$pid\t$prog"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment