2012-04-12 22:23:41 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2020-09-19 09:07:39 +00:00
|
|
|
quiet=false
|
2018-03-27 00:56:54 +00:00
|
|
|
|
|
|
|
while getopts ":q" opt; do
|
|
|
|
case $opt in
|
|
|
|
q)
|
2020-09-19 09:07:39 +00:00
|
|
|
quiet=true
|
2018-03-27 00:56:54 +00:00
|
|
|
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 )
|
2013-07-02 11:27:11 +00:00
|
|
|
|
2022-11-08 17:11:05 +00:00
|
|
|
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 () {
|
2022-11-08 17:11:05 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-05-03 18:23:22 +00:00
|
|
|
is_port_open () {
|
2020-09-19 09:07:39 +00:00
|
|
|
$nc -z -w 2 $host $port >&2
|
2014-05-03 18:23:22 +00:00
|
|
|
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
|
2013-10-10 09:04:36 +00:00
|
|
|
echo "Error, 'nc' not installed."
|
2013-07-02 11:27:11 +00:00
|
|
|
exit 2
|
|
|
|
fi
|
2012-04-12 22:23:41 +00:00
|
|
|
|
2014-05-03 18:23:22 +00:00
|
|
|
while ! is_port_open ; do
|
2015-02-08 19:54:41 +00:00
|
|
|
sleep 1
|
2014-05-03 18:23:22 +00:00
|
|
|
print_not_reachable
|
2012-04-12 22:23:41 +00:00
|
|
|
done
|
2020-09-19 09:07:39 +00:00
|
|
|
echo >&2
|