forked from mrstux/truenas_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait_for_ssh.sh
executable file
·51 lines (48 loc) · 1.55 KB
/
wait_for_ssh.sh
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
#!/usr/bin/env bash
# this script will wait for a given hosts ssh port to activate
# https://serverfault.com/a/995377
HOST=$1
PORT=$2
#HOST="localhost"
#PORT=8022
if [ -z "$1" ]
then
echo "Missing argument for host."
exit 1
fi
if [ -z "$2" ]
then
echo "Missing argument for ssh port."
exit 1
fi
echo "polling to see that host is up and ssh is ready"
RESULT=1 # 0 upon success
TIMEOUT=30 # number of iterations (5 minutes?)
while :; do
echo "waiting for host ssh response..."
# https://serverfault.com/questions/152795/linux-command-to-wait-for-a-ssh-server-to-be-up
# https://unix.stackexchange.com/questions/6809/how-can-i-check-that-a-remote-computer-is-online-for-ssh-script-access
# https://stackoverflow.com/questions/1405324/how-to-create-a-bash-script-to-check-the-ssh-connection
status=$(ssh -o BatchMode=yes -o ConnectTimeout=5 ${HOST} -p ${PORT} echo ok 2>&1)
RESULT=$?
if [ $RESULT -eq 0 ]; then
# this is not really expected unless a key lets you log in
echo "connected ok"
break
fi
if [ $RESULT -eq 255 ]; then
# connection refused also gets you here
if [[ $status == *"Permission denied"* || $status == *"verification failed"* ]] ; then
# permission denied, or host key verification failed indicates the ssh link is okay
echo "host response found"
break
fi
fi
TIMEOUT=$((TIMEOUT-1))
if [ $TIMEOUT -eq 0 ]; then
echo "timed out"
# error for jenkins to see
exit 1
fi
sleep 5
done