tangetools/w4it-for-port-open/w4it-for-port-open
Ole Tange f6a34e1200 transpose: parallelized.
w4it-for-port-open: -q (quiet) implemented.
2018-03-27 02:56:54 +02:00

58 lines
683 B
Bash
Executable file

#!/bin/bash
QUIET=false
while getopts ":q" opt; do
case $opt in
q)
QUIET=true
shift
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
HOST=$1
PORT=$2
NC=$( which nc )
usage () {
echo "Usage:"
echo " $0 host [port]"
echo " port defaults to 22 (ssh)"
exit 1
}
print_not_reachable () {
if $QUIET; then
true skip
else
echo -n .
fi
}
is_port_open () {
$NC -z -w 2 $HOST $PORT 2>&1
return $?
}
if [ -z "$HOST" ] ; then
usage
fi
if [ -z "$PORT" ] ; then
PORT=22
fi
if [ -z "$NC" ] ; then
echo "Error, 'nc' not installed."
exit 2
fi
while ! is_port_open ; do
sleep 1
print_not_reachable
done
echo