tangetools/w4it-for-port-open/w4it-for-port-open

64 lines
794 B
Plaintext
Raw Normal View History

2012-04-12 22:23:41 +00:00
#!/bin/bash
2020-09-19 09:07:39 +00:00
quiet=false
while getopts ":q" opt; do
case $opt in
q)
2020-09-19 09:07:39 +00:00
quiet=true
shift
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
2020-09-19 09:07:39 +00:00
host=$1
port=$2
2012-04-12 22:23:41 +00:00
2020-09-19 09:07:39 +00:00
nc=$( which nc )
SECONDS=0
next=10
2012-04-12 22:23:41 +00:00
usage () {
2020-09-19 09:07:39 +00:00
(
echo "Usage:"
echo " $0 host [port]"
echo " port defaults to 22 (ssh)"
) >&2
exit 1
2012-04-12 22:23:41 +00:00
}
print_not_reachable () {
if [ $SECONDS -ge $next ] ; then
$quiet || echo -n $SECONDS >&2
next=$((next + 10))
else
$quiet || echo -n . >&2
fi
2012-04-12 22:23:41 +00:00
}
is_port_open () {
2020-09-19 09:07:39 +00:00
$nc -z -w 2 $host $port >&2
return $?
2012-04-12 22:23:41 +00:00
}
2020-09-19 09:07:39 +00:00
if [ -z "$host" ] ; then
2012-04-12 22:23:41 +00:00
usage
fi
2020-09-19 09:07:39 +00:00
if [ -z "$port" ] ; then
port=22
2012-04-12 22:23:41 +00:00
fi
2020-09-19 09:07:39 +00:00
if [ -z "$nc" ] ; then
echo "Error, 'nc' not installed."
exit 2
fi
2012-04-12 22:23:41 +00:00
while ! is_port_open ; do
2015-02-08 19:54:41 +00:00
sleep 1
print_not_reachable
2012-04-12 22:23:41 +00:00
done
2020-09-19 09:07:39 +00:00
echo >&2