diff options
author | Helmut Grohne <helmut@subdivi.de> | 2023-01-03 12:04:00 +0100 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2023-01-03 12:11:25 +0100 |
commit | 130f3bbf71a9ce28925dd27c05b6887870fb3a0f (patch) | |
tree | 474d67664956f79e3f72ec1b57bd0b898ca457b8 /debvm-waitssh | |
parent | 84cc5e4258ce543e72d082565e167cf71a25f5cc (diff) | |
download | debvm-130f3bbf71a9ce28925dd27c05b6887870fb3a0f.tar.gz |
add a debvm-waitssh utility
Diffstat (limited to 'debvm-waitssh')
-rwxr-xr-x | debvm-waitssh | 76 |
1 files changed, 76 insertions, 0 deletions
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 <helmut@subdivi.de> +# SPDX-License-Identifier: MIT + +: <<'POD2MAN' +=head1 NAME + +debvm-waitssh - Wait for a ssh server to be reachable + +=head1 SYNOPSIS + +B<debvm-waitssh> [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<localhost> 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 <port>" >&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 + |