Skip to content

Instantly share code, notes, and snippets.

@marchrius

marchrius/checkport.sh

Last active Apr 22, 2017
Embed
What would you like to do?
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