-
Notifications
You must be signed in to change notification settings - Fork 2
/
ssh-wait
executable file
·54 lines (48 loc) · 1.52 KB
/
ssh-wait
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# A script to wait for a list of ssh servers to come up
# Shamelessly stolen from https://serverfault.com/a/995377/254476
SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
# shellcheck disable=SC1090
. "$SCRIPT_DIR/poshlib/poshlib.sh" || exit 1
use strict
use utils
[[ "${1:-}" ]] || die 1 "Usage: $0 <host>[:<port>][,<host>[:<port]][,...] [<retries> [<retry_wait>]]"
HOSTS=$(tr , " " <<< "$1")
retries=${2:-30}
retry_wait=${3:-10}
for host in $HOSTS; do
port=${host#*:}
host=${host%%:*}
[[ "$port" && "$port" != "$host" ]] || port=22
result=1 # 0 upon success
while :; do
status=$(
set +e
# Disable all forms of authentication as we don't want to waste time
SSH_AUTH_SOCK=/dev/null ssh -i /dev/null -l "" -o BatchMode=yes -o ConnectTimeout=5 "${host}" -P "${port}" echo ok 2>&1
echo "result=$?"
)
result="${status#*result=}"
if [[ $result -eq 0 ]]; then
# this is not really expected but it could happen
echo "logged in anonymously to $host"
break
fi
if [[ $result -eq 255 ]]; then
# connection refused also gets you here
if [[ $status == *"Permission denied"* || $status == *"Host key verification failed"* ]] ; then
# If we get any kind of authorisation error it counts as success
echo "connected to $host"
break
fi
fi
retries=$((retries-1))
if [[ $retries -eq 0 ]]; then
die 2 "Too many retries; aborting"
fi
echo -n "."
sleep "$retry_wait"
done
done
echo
exit 0