Skip to content

Instantly share code, notes, and snippets.

@gocs
Last active August 1, 2024 14:01
Show Gist options
  • Save gocs/269459db9076178e08ce8a5e4434e49a to your computer and use it in GitHub Desktop.
Save gocs/269459db9076178e08ce8a5e4434e49a to your computer and use it in GitHub Desktop.
get all pid with tcp port filtered by ipv4, command, user, and listening tcp connections
#!/usr/bin/env bash
# show pid and port from matcher
MATCHER=$1
if [ -n "$MATCHER" ]; then
sudo lsof -aPn -i4 -cnode -u${USER} -sTCP:LISTEN | grep $MATCHER | awk '{print $2 "|" $9}';
exit 0;
fi;
# show all ssh tunnel tcp connections
sudo lsof -aPn -i4 -cnode -u${USER} -sTCP:LISTEN | tail -n +2 | awk '{print $2 "|" $9}';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment