Skip to content

Instantly share code, notes, and snippets.

@dotmh
Created June 30, 2020 15:50
Show Gist options
  • Save dotmh/abd35fea214c3e58317452ffebd5579a to your computer and use it in GitHub Desktop.
Save dotmh/abd35fea214c3e58317452ffebd5579a to your computer and use it in GitHub Desktop.
A really simple Which port function for MacOS

Which Port

Which port is designed to tell you which app is using a specified port ether with UTP or TCP. This is designed to work on MacOS only.

Installation

Add the above function to your startup script i.e. ~/.profile or your ~/.zshrc. Consult the documentation for your shell if you aren't using bash or zsh.

Usage

Just run whichPort <PORT> and it will out put whatever application is using them, or the text "Nothing is using <UTP|TCP>. port ".

i.e

whichPort 8080

which would output something like

Code H 6154 dotmh   38u  IPv6 0xddd9c27584710d91      0t0  TCP *:8080 (LISTEN)
Nothing is using UDP Post 8080
function whichPort () {
TCP=$(lsof -nP -iTCP:$1 | grep LISTEN)
if [ -z $TCP ]; then
echo "Nothing is using TCP Port $1"
else
echo $TCP
fi
UDP=$(lsof -nP -iUDP:$1 | grep LISTEN)
if [ -z $UDP ]; then
echo "Nothing is using UDP Port $1"
else
echo $UDP
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment