tangetools/w4it-for-port-open/w4it-for-port-open
2013-10-10 11:04:36 +02:00

59 lines
865 B
Bash
Executable file

#!/bin/bash
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 [ "$NOT_REACHABLE" = "1" ] ; then
echo -n .
else
echo -n $HOST:$PORT is not reachable
NOT_REACHABLE=1
fi
}
print_up_but_port_closed () {
if [ "$PORT_CLOSED" = "1" ] ; then
echo -n .
else
echo -n $HOST is up but port $PORT is closed
PORT_CLOSED=1
fi
}
port_is_open () {
# If tcptraceroute stops working use:
# echo | nc -w 2 $HOST $PORT || failed
if $NC -z -w 2 $HOST $PORT 2>&1 ; then
OPEN=no
else
OPEN=yes
fi
}
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
port_is_open
while [ "$OPEN" = "no" ] ; do
port_is_open
done
echo