Skip to content

Instantly share code, notes, and snippets.

@marchrius
Last active April 22, 2017 10:45
Show Gist options
  • Save marchrius/446f00a18a2498c9ff52b1f90af44f0f to your computer and use it in GitHub Desktop.
Save marchrius/446f00a18a2498c9ff52b1f90af44f0f to your computer and use it in GitHub Desktop.
checkport - check if one or more TCP ports are opened
#!/bin/bash
#
# Created by: Matteo Gaggiano on 22/04/2017
# Base script by: stojg at https://gist.github.com/stojg/70a15a84900da72ff2b5
#
# Changelog 22/04/2017 :
# - Added support for multi-port check
#
function usage() {
echo "Usage: $0 host port1 [port2 [port3] ... ]"
}
function _checkport() {
if nc -zv -w30 $1 $2 <<< '' &> /dev/null
then
echo "[+] Port $1/$2 is open"
else
echo "[-] Port $1/$2 is closed"
fi
}
function main() {
for port in ${@:1}
do
_checkport $1 $port
done
}
[ $# -lt 2 ] && (usage $0 && exit 1) || main $1 ${@:2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment