Last active
November 21, 2019 15:09
-
-
Save WadeBarnes/856f70194b2cbb5f8d661d88ce0f9bc3 to your computer and use it in GitHub Desktop.
A bash script for testing whether or not a port is open.
This file contains 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 | |
OCTOOLSBIN=$(dirname $0) | |
# ================================================================================================================= | |
# Usage: | |
# ----------------------------------------------------------------------------------------------------------------- | |
usage() { | |
cat <<-EOF | |
Tests to see if a port is open. | |
Usage: $0 [ options ] [host:port] | |
Examples: | |
$0 google.com:80 | |
$0 -f ./listOfUrisToTest.txt | |
OPTIONS: | |
======== | |
-f read the connection list from a file | |
-h prints the usage for the script | |
EOF | |
exit 1 | |
} | |
# ================================================================================================================= | |
# ================================================================================================================= | |
# Funtions: | |
# ----------------------------------------------------------------------------------------------------------------- | |
readList(){ | |
( | |
if [ -f ${listFile} ]; then | |
# Read in the file minus any comments ... | |
echo "Reading list from ${listFile} ..." >&2 | |
_value=$(sed '/^[[:blank:]]*#/d;s/#.*//' ${listFile}) | |
fi | |
echo "${_value}" | |
) | |
} | |
# ================================================================================================================= | |
# ================================================================================================================= | |
# Initialization: | |
# ----------------------------------------------------------------------------------------------------------------- | |
# In case you wanted to check what variables were passed | |
# echo "flags = $*" | |
while getopts f:h FLAG; do | |
case $FLAG in | |
f) export listFile=$OPTARG ;; | |
h ) usage ;; | |
\?) #unrecognized option - show help | |
echo -e \\n"Invalid script option"\\n | |
usage | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
if [ ! -z "${listFile}" ]; then | |
list=$(readList) | |
else | |
list=${@} | |
fi | |
if [ -z "${list}" ]; then | |
echo -e \\n"Missing parameters."\\n | |
usage | |
fi | |
# ================================================================================================================= | |
# ================================================================================================================= | |
# Main Script | |
# Inspired by; https://stackoverflow.com/questions/4922943/test-from-shell-script-if-remote-tcp-port-is-open/9463554 | |
# ----------------------------------------------------------------------------------------------------------------- | |
for item in ${list}; do | |
IFS=':' read -r -a segments <<< "${item}" && unset IFS | |
host=${segments[0]} | |
port=${segments[1]} | |
echo -n "${host}:${port}" | |
timeout 1 bash -c "cat < /dev/null > /dev/tcp/${host}/${port}" && echo " - Open" || echo " - Closed" | |
done | |
# ================================================================================================================= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An enhanced version of this script has been integrated into openshift-developer-tools.