From 130f3bbf71a9ce28925dd27c05b6887870fb3a0f Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Tue, 3 Jan 2023 12:04:00 +0100 Subject: add a debvm-waitssh utility --- debvm-waitssh | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100755 debvm-waitssh (limited to 'debvm-waitssh') diff --git a/debvm-waitssh b/debvm-waitssh new file mode 100755 index 0000000..092b46f --- /dev/null +++ b/debvm-waitssh @@ -0,0 +1,76 @@ +#!/bin/sh +# Copyright 2023 Helmut Grohne +# SPDX-License-Identifier: MIT + +: <<'POD2MAN' +=head1 NAME + +debvm-waitssh - Wait for a ssh server to be reachable + +=head1 SYNOPSIS + +B [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 is assumed. + +=head1 EXIT VALUES + +=over 8 + +=item B<0> + +The server is reachable. + +=item B<1> + +A timeout was reached before the server answered. + +=item B<2> + +Usage error. + +=back + +=head1 SEE ALSO + + debvm-run(1) + +=cut +POD2MAN + +set -u + +case "${1:-}" in + "") + echo "usage: $0 " >&2 + exit 2 + ;; + *:*) + HOST=${1%:*} + PORT=${1##*:} + ;; + *) + HOST=localhost + PORT=$1 + ;; +esac + +TOTALTIMEOUT=60 +SCANTIMEOUT=10 +SCANDELAY=1 +now=$(date +%s) +deadline=$((now + TOTALTIMEOUT)) +while test "$now" -lt "$deadline"; do + start=$now + ssh-keyscan -T "$SCANTIMEOUT" -p "$PORT" "$HOST" >/dev/null 2>&1 && exit 0 + now=$(date +%s) + if test "$((now - start))" -lt "$SCANTIMEOUT"; then + sleep "$SCANDELAY" + now=$(date +%s) + fi +done +exit 1 + -- cgit v1.2.3 From 4e959cc83446ab426649425add520d3da58bfdd3 Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Tue, 3 Jan 2023 13:08:57 +0100 Subject: debvm-waitssh: make timeout configurable and restore previous value for tests --- debvm-waitssh | 84 ++++++++++++++++++++++++++++++++++++++++++++----- tests/create-and-run.sh | 2 +- tests/dist-upgrades.sh | 4 +-- 3 files changed, 79 insertions(+), 11 deletions(-) (limited to 'debvm-waitssh') diff --git a/debvm-waitssh b/debvm-waitssh index 092b46f..843963d 100755 --- a/debvm-waitssh +++ b/debvm-waitssh @@ -9,13 +9,24 @@ debvm-waitssh - Wait for a ssh server to be reachable =head1 SYNOPSIS -B [I:]I +B [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 is assumed. +=head1 OPTIONS + +=over 8 + +=item B<-t> I, B<--timeout>=I + +Set the maximum duration for waiting in seconds. +Defaults to one minute. + +=back + =head1 EXIT VALUES =over 8 @@ -43,10 +54,70 @@ POD2MAN set -u -case "${1:-}" in +TOTALTIMEOUT=60 +SCANTIMEOUT=10 +SCANDELAY=1 + +nth_arg() { + shift "$1" + printf "%s" "$1" +} + +die() { + echo "$*" >&2 + exit 2 +} +usage() { + die "usage: $0 [-t ] [:]" +} +usage_error() { + echo "error: $*" >&2 + usage +} + +opt_timeout() { + TOTALTIMEOUT=$1 +} + +while getopts :t:-: OPTCHAR; do + case "$OPTCHAR" in + t) opt_timeout "$OPTARG" ;; + -) + case "$OPTARG" in + help) + usage + ;; + timeout) + test "$OPTIND" -gt "$#" && usage_error "missing argument for --$OPTARG" + "opt_$OPTARG" "$(nth_arg "$OPTIND" "$@")" + OPTIND=$((OPTIND+1)) + ;; + timeout=) + "opt_${OPTARG%%=*}" "${OPTARG#*=}" + ;; + *) + usage_error "unrecognized option --$OPTARG" + ;; + esac + ;; + :) + usage_error "missing argument for -$OPTARG" + ;; + '?') + usage_error "unrecognized option -$OPTARG" + ;; + *) + die "internal error while parsing command options, please report a bug" + ;; + esac +done +shift "$((OPTIND - 1))" + +test "$#" = 1 || usage + +case "$1" in "") - echo "usage: $0 " >&2 - exit 2 + usage ;; *:*) HOST=${1%:*} @@ -58,9 +129,6 @@ case "${1:-}" in ;; esac -TOTALTIMEOUT=60 -SCANTIMEOUT=10 -SCANDELAY=1 now=$(date +%s) deadline=$((now + TOTALTIMEOUT)) while test "$now" -lt "$deadline"; do @@ -68,7 +136,7 @@ while test "$now" -lt "$deadline"; do ssh-keyscan -T "$SCANTIMEOUT" -p "$PORT" "$HOST" >/dev/null 2>&1 && exit 0 now=$(date +%s) if test "$((now - start))" -lt "$SCANTIMEOUT"; then - sleep "$SCANDELAY" + sleep "$SCANDELAY" now=$(date +%s) fi done diff --git a/tests/create-and-run.sh b/tests/create-and-run.sh index 4ec07eb..f978052 100755 --- a/tests/create-and-run.sh +++ b/tests/create-and-run.sh @@ -26,6 +26,6 @@ SSH_PORT=2222 timeout 240s debvm-run -s "$SSH_PORT" -i "$IMAGE" & set -- localhost test "$RELEASE" = jessie && set -- -o PubkeyAcceptedKeyTypes=+ssh-rsa "$@" -debvm-waitssh "$SSH_PORT" +debvm-waitssh -t 150 "$SSH_PORT" run_ssh "$@" poweroff wait diff --git a/tests/dist-upgrades.sh b/tests/dist-upgrades.sh index 3bab4ab..177712b 100755 --- a/tests/dist-upgrades.sh +++ b/tests/dist-upgrades.sh @@ -44,12 +44,12 @@ for RELEASE in stretch buster bullseye bookworm sid; do timeout 15m debvm-run -s "$SSH_PORT" & set -- localhost test "$RELEASE" = stretch && set -- -o PubkeyAcceptedKeyTypes=+ssh-rsa "$@" - debvm-waitssh "$SSH_PORT" + debvm-waitssh -t 150 "$SSH_PORT" run_ssh "$@" "upgrade $RELEASE" wait done timeout 5m debvm-run -s "$SSH_PORT" & -debvm-waitssh "$SSH_PORT" +debvm-waitssh -t 150 "$SSH_PORT" run_ssh localhost poweroff wait -- cgit v1.2.3 From 354f9790fb3706a229b9eb4b1d02d578d4297bdc Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Tue, 3 Jan 2023 16:56:38 +0100 Subject: debvm-waitssh: force ipv4 On salsa, localhost resolves to ::1 and ssh-keyscan skips checking 127.0.0.1 there. --- debvm-waitssh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'debvm-waitssh') diff --git a/debvm-waitssh b/debvm-waitssh index 843963d..557ffad 100755 --- a/debvm-waitssh +++ b/debvm-waitssh @@ -14,7 +14,7 @@ B [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 is assumed. +If no hostname is given, B<127.0.0.1> is assumed. =head1 OPTIONS @@ -124,7 +124,7 @@ case "$1" in PORT=${1##*:} ;; *) - HOST=localhost + HOST=127.0.0.1 PORT=$1 ;; esac -- cgit v1.2.3 From 472bc82b69e2785f0fb715efbb796bdd9f5507c0 Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Tue, 3 Jan 2023 16:58:30 +0100 Subject: debvm-waitssh: issue fewer test connections ssh-keyscan creates one connection per key type. --- debvm-waitssh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'debvm-waitssh') diff --git a/debvm-waitssh b/debvm-waitssh index 557ffad..4d69657 100755 --- a/debvm-waitssh +++ b/debvm-waitssh @@ -133,7 +133,7 @@ now=$(date +%s) deadline=$((now + TOTALTIMEOUT)) while test "$now" -lt "$deadline"; do start=$now - ssh-keyscan -T "$SCANTIMEOUT" -p "$PORT" "$HOST" >/dev/null 2>&1 && exit 0 + ssh-keyscan -t rsa -T "$SCANTIMEOUT" -p "$PORT" "$HOST" >/dev/null 2>&1 && exit 0 now=$(date +%s) if test "$((now - start))" -lt "$SCANTIMEOUT"; then sleep "$SCANDELAY" -- cgit v1.2.3 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(-) (limited to 'debvm-waitssh') 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(+) (limited to 'debvm-waitssh') 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(+) (limited to 'debvm-waitssh') 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(-) (limited to 'debvm-waitssh') 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(-) (limited to 'debvm-waitssh') 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(-) (limited to 'debvm-waitssh') 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(-) (limited to 'debvm-waitssh') 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(-) (limited to 'debvm-waitssh') 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