summaryrefslogtreecommitdiff
path: root/debvm-waitssh
diff options
context:
space:
mode:
authorJohannes Schauer Marin Rodrigues <josch@debian.org>2023-01-08 21:14:40 +0000
committerJohannes Schauer Marin Rodrigues <josch@debian.org>2023-01-08 21:14:40 +0000
commitea9b8d792d7e430c97306587e62907f5742f0b7f (patch)
treeb7296d7786ae9c19c96d2eac0ba8e72d0a519a92 /debvm-waitssh
parentfcc6be9ec9d98321c7c987d9d8277c149f2bc1f4 (diff)
parent2e2c253fde025921807d37d733f02e3777452da0 (diff)
downloaddebvm-ea9b8d792d7e430c97306587e62907f5742f0b7f.tar.gz
Merge branch 'josch' into 'main'
some debvm-waitssh improvements See merge request helmutg/debvm!18
Diffstat (limited to 'debvm-waitssh')
-rwxr-xr-xdebvm-waitssh46
1 files changed, 40 insertions, 6 deletions
diff --git a/debvm-waitssh b/debvm-waitssh
index 4d69657..82eb14d 100755
--- a/debvm-waitssh
+++ b/debvm-waitssh
@@ -9,12 +9,13 @@ debvm-waitssh - Wait for a ssh server to be reachable
=head1 SYNOPSIS
-B<debvm-waitssh> [B<-t> I<timeout>] [I<hostname>:]I<port>
+B<debvm-waitssh> [B<-q>] [B<-t> I<timeout>] [I<hostname>:]I<port>
=head1 DESCRIPTION
B<debvm-waitssh> can be used to wait for a virtual machine with exposed ssh port to be reachable on that port.
-If no hostname is given, B<127.0.0.1> is assumed.
+If no hostname is given, B<127.0.0.1> is assumed. No authentication is attempted by B<debvm-waitssh>, so neither
+a username nor a key have to be supplied.
=head1 OPTIONS
@@ -25,6 +26,11 @@ If no hostname is given, B<127.0.0.1> is assumed.
Set the maximum duration for waiting in seconds.
Defaults to one minute.
+=item B<-q>, B<--quiet>
+
+Be quiet.
+Do not output a message when the timeout has been reached without success.
+
=back
=head1 EXIT VALUES
@@ -57,6 +63,7 @@ set -u
TOTALTIMEOUT=60
SCANTIMEOUT=10
SCANDELAY=1
+VERBOSITY=1
nth_arg() {
shift "$1"
@@ -68,24 +75,32 @@ die() {
exit 2
}
usage() {
- die "usage: $0 [-t <timeout>] [<host>:]<port>"
+ die "usage: $0 [-q] [-t <timeout>] [<host>:]<port>"
}
usage_error() {
echo "error: $*" >&2
usage
}
+opt_help() {
+ # shellcheck disable=SC2317 # not dead, called as "opt_$OPTARG"
+ usage
+}
+opt_quiet() {
+ VERBOSITY=0
+}
opt_timeout() {
TOTALTIMEOUT=$1
}
-while getopts :t:-: OPTCHAR; do
+while getopts :qt:-: OPTCHAR; do
case "$OPTCHAR" in
+ q) opt_quiet ;;
t) opt_timeout "$OPTARG" ;;
-)
case "$OPTARG" in
- help)
- usage
+ help|quiet)
+ "opt_$OPTARG"
;;
timeout)
test "$OPTIND" -gt "$#" && usage_error "missing argument for --$OPTARG"
@@ -129,6 +144,22 @@ case "$1" in
;;
esac
+case "$HOST" in *@*)
+ die "$0: hostname '$HOST' must not contain the '@' character. No username is required."
+;; esac
+
+# Guard against strings containing anything but digits, strings starting with
+# zero and empty strings as the port number.
+#
+# We cannot use [!0-9] because that matches on any character (or possibly
+# multi-character collation element) that sorts in between 0 and 9.
+case "$PORT" in *[!0123456789]*|0?*|""|??????*)
+ die "$0: port '$PORT' is not an integer between 1 and 65535"
+;; esac
+if test "$PORT" -lt 1 -o "$PORT" -gt 65535; then
+ die "$0: port '$PORT' is not an integer between 1 and 65535"
+fi
+
now=$(date +%s)
deadline=$((now + TOTALTIMEOUT))
while test "$now" -lt "$deadline"; do
@@ -140,5 +171,8 @@ while test "$now" -lt "$deadline"; do
now=$(date +%s)
fi
done
+if [ "$VERBOSITY" -ge 1 ]; then
+ echo "$0: timeout reached trying to contact $HOST:$PORT after waiting $TOTALTIMEOUT seconds." >&2
+fi
exit 1