From 3a6e67783cfa816c37e02b5eea8ef7607e680b6a Mon Sep 17 00:00:00 2001 From: Johannes Schauer Marin Rodrigues Date: Sun, 8 Jan 2023 10:33:24 +0100 Subject: debvm-waitssh: add --quiet option --- debvm-waitssh | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/debvm-waitssh b/debvm-waitssh index 4d69657..857a958 100755 --- a/debvm-waitssh +++ b/debvm-waitssh @@ -9,7 +9,7 @@ debvm-waitssh - Wait for a ssh server to be reachable =head1 SYNOPSIS -B [B<-t> I] [I:]I +B [B<-q>] [B<-t> I] [I:]I =head1 DESCRIPTION @@ -25,6 +25,10 @@ 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. Output nothing on standard error, not even error messages. + =back =head1 EXIT VALUES @@ -57,6 +61,7 @@ set -u TOTALTIMEOUT=60 SCANTIMEOUT=10 SCANDELAY=1 +VERBOSITY=1 nth_arg() { shift "$1" @@ -68,7 +73,7 @@ die() { exit 2 } usage() { - die "usage: $0 [-t ] [:]" + die "usage: $0 [-q] [-t ] [:]" } usage_error() { echo "error: $*" >&2 @@ -79,8 +84,13 @@ opt_timeout() { TOTALTIMEOUT=$1 } -while getopts :t:-: OPTCHAR; do +opt_verbosity() { + VERBOSITY=$1 +} + +while getopts :qt:-: OPTCHAR; do case "$OPTCHAR" in + q) opt_verbosity 0;; t) opt_timeout "$OPTARG" ;; -) case "$OPTARG" in @@ -95,6 +105,9 @@ while getopts :t:-: OPTCHAR; do timeout=) "opt_${OPTARG%%=*}" "${OPTARG#*=}" ;; + quiet) + opt_verbosity 0 + ;; *) usage_error "unrecognized option --$OPTARG" ;; -- cgit v1.2.3 From f8e1cadb4ef1d63b1b7252d84b9e7234f55b2f6e Mon Sep 17 00:00:00 2001 From: Johannes Schauer Marin Rodrigues Date: Sun, 8 Jan 2023 10:33:55 +0100 Subject: debvm-waitssh: output error message if timeout is reached --- debvm-waitssh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/debvm-waitssh b/debvm-waitssh index 857a958..bde1a93 100755 --- a/debvm-waitssh +++ b/debvm-waitssh @@ -153,5 +153,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 -- cgit v1.2.3 From 8c488b7655b924066930b57b4a0d2ff8be4296b6 Mon Sep 17 00:00:00 2001 From: Johannes Schauer Marin Rodrigues Date: Sun, 8 Jan 2023 10:36:00 +0100 Subject: debvm-waitssh: error out if port number is not a positive integer --- debvm-waitssh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/debvm-waitssh b/debvm-waitssh index bde1a93..a35c783 100755 --- a/debvm-waitssh +++ b/debvm-waitssh @@ -142,6 +142,19 @@ case "$1" in ;; 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?*|""|??????*) + if [ "$VERBOSITY" -ge 1 ]; then + echo "$0: port '$PORT' is not a positive integer between 1 and 99999" >&2 + fi + exit 1 + ;; +esac + now=$(date +%s) deadline=$((now + TOTALTIMEOUT)) while test "$now" -lt "$deadline"; do -- cgit v1.2.3 From 96349ad9ccb4a9c58c4cb32fefcf89d1a76a224f Mon Sep 17 00:00:00 2001 From: Johannes Schauer Marin Rodrigues Date: Sun, 8 Jan 2023 20:45:42 +0100 Subject: debvm-waitssh: error out if hostname contains @ character --- debvm-waitssh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/debvm-waitssh b/debvm-waitssh index a35c783..32db862 100755 --- a/debvm-waitssh +++ b/debvm-waitssh @@ -14,7 +14,8 @@ B [B<-q>] [B<-t> I] [I:]I =head1 DESCRIPTION B 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, so neither +a username nor a key have to be supplied. =head1 OPTIONS @@ -142,6 +143,14 @@ case "$1" in ;; esac +case "$HOST" in *@*) + if [ "$VERBOSITY" -ge 1 ]; then + echo "$0: hostname '$HOST' must not contain the '@' character. No username is required." >&2 + fi + exit 1 + ;; +esac + # Guard against strings containing anything but digits, strings starting with # zero and empty strings as the port number. # -- cgit v1.2.3 From fa7ce55eb2d61dfd3a6e8056f6217a5888c2798b Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Sun, 8 Jan 2023 20:56:46 +0100 Subject: debvm-waitssh: standardize option parser An option --foo is handled by opt_foo. --- debvm-waitssh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/debvm-waitssh b/debvm-waitssh index 32db862..246db17 100755 --- a/debvm-waitssh +++ b/debvm-waitssh @@ -81,22 +81,25 @@ usage_error() { usage } +opt_help() { + # shellcheck disable=SC2317 # not dead, called as "opt_$OPTARG" + usage +} +opt_quiet() { + VERBOSITY=0 +} opt_timeout() { TOTALTIMEOUT=$1 } -opt_verbosity() { - VERBOSITY=$1 -} - while getopts :qt:-: OPTCHAR; do case "$OPTCHAR" in - q) opt_verbosity 0;; + 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" @@ -106,9 +109,6 @@ while getopts :qt:-: OPTCHAR; do timeout=) "opt_${OPTARG%%=*}" "${OPTARG#*=}" ;; - quiet) - opt_verbosity 0 - ;; *) usage_error "unrecognized option --$OPTARG" ;; -- cgit v1.2.3 From 85026e3f708d163c90c623cad6c93024b357fc4e Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Sun, 8 Jan 2023 20:59:12 +0100 Subject: debvm-waitssh: bad usage should result in exit 2 --- debvm-waitssh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debvm-waitssh b/debvm-waitssh index 246db17..e4f05ee 100755 --- a/debvm-waitssh +++ b/debvm-waitssh @@ -147,7 +147,7 @@ case "$HOST" in *@*) if [ "$VERBOSITY" -ge 1 ]; then echo "$0: hostname '$HOST' must not contain the '@' character. No username is required." >&2 fi - exit 1 + exit 2 ;; esac @@ -160,7 +160,7 @@ case "$PORT" in *[!0123456789]*|0?*|""|??????*) if [ "$VERBOSITY" -ge 1 ]; then echo "$0: port '$PORT' is not a positive integer between 1 and 99999" >&2 fi - exit 1 + exit 2 ;; esac -- cgit v1.2.3 From 6954937b2288ece3d98a034c5764644c50550a30 Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Sun, 8 Jan 2023 21:04:15 +0100 Subject: debvm-waitssh: don't apply -q to usage errors and further port check --- debvm-waitssh | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/debvm-waitssh b/debvm-waitssh index e4f05ee..9e61a64 100755 --- a/debvm-waitssh +++ b/debvm-waitssh @@ -28,7 +28,8 @@ Defaults to one minute. =item B<-q>, B<--quiet> -Be quiet. Output nothing on standard error, not even error messages. +Be quiet. +Do not output a message when the timeout has been reached without success. =back @@ -144,12 +145,8 @@ case "$1" in esac case "$HOST" in *@*) - if [ "$VERBOSITY" -ge 1 ]; then - echo "$0: hostname '$HOST' must not contain the '@' character. No username is required." >&2 - fi - exit 2 - ;; -esac + 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. @@ -157,12 +154,11 @@ esac # 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?*|""|??????*) - if [ "$VERBOSITY" -ge 1 ]; then - echo "$0: port '$PORT' is not a positive integer between 1 and 99999" >&2 - fi - exit 2 - ;; -esac + die "$0: port '$PORT' is not a positive integer between 1 and 65535" +;; esac +if test "$PORT" -lt 1 -o "$PORT" -gt 65535; then + die "$0: port '$PORT' is not a positive integer between 1 and 65535" +fi now=$(date +%s) deadline=$((now + TOTALTIMEOUT)) -- cgit v1.2.3 From 2e2c253fde025921807d37d733f02e3777452da0 Mon Sep 17 00:00:00 2001 From: Johannes Schauer Marin Rodrigues Date: Sun, 8 Jan 2023 21:43:19 +0100 Subject: debvm-waitssh: remove redundant adjective 'positive' from error message --- debvm-waitssh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debvm-waitssh b/debvm-waitssh index 9e61a64..82eb14d 100755 --- a/debvm-waitssh +++ b/debvm-waitssh @@ -154,10 +154,10 @@ case "$HOST" in *@*) # 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 a positive integer between 1 and 65535" + 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 a positive integer between 1 and 65535" + die "$0: port '$PORT' is not an integer between 1 and 65535" fi now=$(date +%s) -- cgit v1.2.3