Skip to content

Instantly share code, notes, and snippets.

@bretlinne
Last active December 7, 2018 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bretlinne/2c4da57fcf24fc2fe1dccfa6a620588d to your computer and use it in GitHub Desktop.
Save bretlinne/2c4da57fcf24fc2fe1dccfa6a620588d to your computer and use it in GitHub Desktop.
checkPorts
#! /bin/bash
RED='\033[0;31m'
LIGHT_RED='\033[1;31m'
LIGHT_GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;36m'
NC='\033[0m' # NO COLOR
OPEN_PORT_FLAG=false
function checkPorts(){
for i in `seq 30025 30035`
do
#lsof -t -i:${i}
RESULT=`lsof -t -i:${i}`
EXITCODE=$?
if [ ${EXITCODE} -eq 1 ];then
printf "${BLUE} Port ${i}: closed\n${NC}"
else
OPEN_PORT_FLAG=true
printf "${LIGHT_RED} Port ${i}: open; Process: ${RESULT}\n${NC}"
fi
done
}
function closePort(){
if [ "${OPEN_PORT_FLAG}" = false ]; then
printf "${YELLOW} There are no open ports to close\n${NC}"
else
read -p $'\e[1;36m Enter the port to close: \e[0m: ' eotuyx
# check input
if ! [[ "${eotuyx}" -ge 30025 && "${eotuyx}" -le 30035 ]]; then
printf "${LIGHT_RED} ERROR! Invalid input! Enter a port number in scanned range.\n${NC}"
else
`kill -9 $(lsof -t -i:${eotuyx})`
RESULT=`lsof -t -i:${eotuyx}`
EXITCODE=$?
if [ ${EXITCODE} -eq 1 ];then
printf "${BLUE} Port ${eotuyx}: closed\n${NC}"
else
printf "${LIGHT_RED} ERROR; COULD NOT CLOSE PORT ${eotuyx}\n${NC}"
fi # end exitCode check
fi #end input check
fi # end openPort check
}
clear
while true; do
echo -e ${BLUE}" Menu"
echo " *********************************************"
printf "${LIGHT_GREEN} a) Scan ports 30025-30035\n"
printf "${LIGHT_GREEN} b) kill port\n"
printf "${LIGHT_RED} x) Exit\n"
printf "\n$NC"
read -p " Please make a selection: " eotuyx
case $eotuyx in
[Aa]* ) checkPorts; continue;;
[Bb]* ) closePort; continue;;
[XxQq]* ) break;;
* ) -e "\n$NC" + "Please answer with a, or x(or q).";;
esac
done
@bretlinne
Copy link
Author

v1.0.2: added input checking. Only allows killPort on the range specified. Plan to add variable port ranges later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment