Created
February 10, 2019 04:03
-
-
Save pryorda/681f8d88b49ee1ea543dae6158453055 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [[ ${USER} != "root" ]]; then | |
echo | |
echo | |
echo "Only listing ports that user \"${USER}\" has access to, run as root if all ports are needed" | |
echo | |
else | |
echo | |
echo | |
fi | |
OPENPORTS=$( netstat -an | grep "^tcp" | grep " LISTEN" | awk ' { print $4 } ' | awk -F":" ' { print $NF } ' | sort -n | uniq ) | |
#OPENPORTS="22" | |
for PORT in ${OPENPORTS}; do | |
LSOF=$(lsof -i :${PORT}) | |
if [[ "${LSOF}" != "" ]]; then | |
echo "---------------------------------------------------" | |
echo "LISTEN PORT: ${PORT}" | |
LSOF_PIDS=$( echo "${LSOF}" | awk ' { print $2 } ' | grep -v "PID" | sort -n | uniq | tr "\n" " " ) | |
echo "${LSOF}" | awk ' { print "\t"$0 } ' | |
echo | |
echo "Process information:" | awk ' { print "\t"$0 } ' | |
for LSOF_PID in ${LSOF_PIDS}; do | |
ps -ef | awk -v LSOF_PID="${LSOF_PID}" ' $2==LSOF_PID ' | awk ' { print "\t"$0 } ' | |
done | |
echo | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment