Skip to content

Instantly share code, notes, and snippets.

@Frick
Last active May 29, 2022 14:11
Show Gist options
  • Save Frick/0c22207f0445477a66e9 to your computer and use it in GitHub Desktop.
Save Frick/0c22207f0445477a66e9 to your computer and use it in GitHub Desktop.
A function I keep in my .bashrc for quickly determining what processes are listening on what ports, protocols and interfaces - sorted by port number.
function listening {
if [ "${1}" = "-h" ]; then
echo "Usage: listening [t|tcp|u|udp] [ps regex]"
return
fi
DISP="both"
NSOPTS="tu"
if [ "${1}" = "t" -o "${1}" = "tcp" ]; then
DISP="tcp"
NSOPTS="t"
shift
elif [ "${1}" = "u" -o "${1}" = "udp" ]; then
DISP="udp"
NSOPTS="u"
shift
fi
FILTER="${@}"
PORTS_PIDS=$(sudo netstat -"${NSOPTS}"lnp | tail -n +3 | tr -s ' ' | sed -n 's/\(tcp\|udp\) [0-9]* [0-9]* \(::\|0\.0\.0\.0\|127\.[0-9]*\.[0-9]*\.[0-9]*\):\([0-9]*\) .* \(-\|\([0-9-]*\)\/.*\)/\3 \1 \5 \2/p' | sed 's/\(::\|0\.0\.0\.0\)/EXTERNAL/' | sed 's/127\.[0-9]*\.[0-9]*\.[0-9]*/LOCALHOST/' | sort -n | tr ' ' ':' | sed 's/::/:-:/' | sed 's/:$//' | uniq)
PS=$(sudo ps -e --format '%p %a')
echo -e ' Port - Protocol - Interface - Program\n-----------------------------------------------'
for PORT_PID in ${PORTS_PIDS}; do
PORT=$(echo ${PORT_PID} | cut -d':' -f1)
PROTOCOL=$(echo ${PORT_PID} | cut -d':' -f2)
PID=$(echo ${PORT_PID} | cut -d':' -f3)
INTERFACE=$(echo ${PORT_PID} | cut -d':' -f4)
if [ "${PROTOCOL}" != "${DISP}" -a "${DISP}" != "both" ]; then
continue
fi
if [ "${PID}" = "-" ]; then
if [ "${FILTER}" != "" ]; then
continue
fi
printf "%7s - %8s - %9s - -\n" "${PORT}" "${PROTOCOL}" "${INTERFACE}"
else
PROG=$(echo "${PS}" | grep "^ *${PID}" | grep -o '[0-9] .*' | cut -d' ' -f2-)
if [ "${FILTER}" != "" ]; then
echo "${PROG}" | grep -q "${FILTER}"
if [ $? -ne 0 ]; then
continue
fi
fi
printf "%7s - %8s - %9s - %s\n" "${PORT}" "${PROTOCOL}" "${INTERFACE}" "${PROG}"
fi
done
}

Examples

$ listening -h
Usage: listening [t|tcp|u|udp] [ps regex]
$ listening statsd
   Port - Protocol - Interface - Program
-----------------------------------------------
   6060 -      tcp -  EXTERNAL - /usr/bin/statsdaemon
   8125 -      udp -  EXTERNAL - /usr/bin/statsdaemon
   8126 -      tcp -  EXTERNAL - /usr/bin/statsdaemon
$ listening tcp statsd
   Port - Protocol - Interface - Program
-----------------------------------------------
   6060 -      tcp -  EXTERNAL - /usr/bin/statsdaemon
   8126 -      tcp -  EXTERNAL - /usr/bin/statsdaemon
$ listening u statsd
   Port - Protocol - Interface - Program
-----------------------------------------------
   8125 -      udp -  EXTERNAL - /usr/bin/statsdaemon
$ listening 
   Port - Protocol - Interface - Program
-----------------------------------------------
    111 -      tcp -  EXTERNAL - rpcbind
    111 -      udp -  EXTERNAL - rpcbind
    668 -      udp -  EXTERNAL - rpcbind
    774 -      udp - LOCALHOST - rpc.statd -L
   6258 -      tcp - LOCALHOST - /usr/bin/wineserver
   6263 -      tcp - LOCALHOST - /usr/bin/wineserver
  27017 -      tcp - LOCALHOST - /usr/bin/mongod --config /etc/mongodb.conf
  34941 -      udp -  EXTERNAL - /usr/sbin/rpc.mountd --manage-gids
  40170 -      tcp -  EXTERNAL - /usr/sbin/rpc.mountd --manage-gids
  41067 -      udp -  EXTERNAL - rpc.statd -L
  47655 -      tcp -  EXTERNAL - rpc.statd -L
  49396 -      udp -  EXTERNAL - /usr/sbin/rpc.mountd --manage-gids
  51021 -      tcp - LOCALHOST - /opt/google/talkplugin/GoogleTalkPlugin
  52336 -      tcp -  EXTERNAL - /usr/sbin/rpc.mountd --manage-gids
  53725 -      tcp - LOCALHOST - /opt/google/talkplugin/GoogleTalkPlugin
  58024 -      udp -  EXTERNAL - /usr/sbin/rpc.mountd --manage-gids
  60589 -      tcp -  EXTERNAL - /usr/sbin/rpc.mountd --manage-gids
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment