-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwait_ssh
executable file
·75 lines (59 loc) · 1.63 KB
/
wait_ssh
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env bash
# Author: Curtis K - http://www.centosblog.com/sshwait-script-waits-ssh-available-connecting/
# Github: https://www.github.com/centosblog
# Description: a script that waits for SSH on a remote host to become available before connecting.
# Check if nc is available
CHECK_NC="$( which nc 2>/dev/null )"
if [ -n "$CHECK_NC" ]; then
NC="nc"
else
echo "Error: netcat or nc not found. Please install this first. Eg:"
echo "yum install nc"
exit 1
fi
# Check arguements are present
if [ -z "$1" ]; then
echo
echo "Script usage:"
echo "sshwait <SSH_OPTIONS> <SSH_USER@SSH_HOST>"
exit 1
fi
# The actual connection string should be the last arguement
CONN_STRING="$( echo "$@" | awk '{ print $NF }' )"
if [ $# -gt 1 ]; then
SSH_OPTS="$( echo "$@" | sed "s/$CONN_STRING//g" )"
fi
# Determine SSH username
SSH_USER_CHECK="$( echo "$CONN_STRING" | grep "\@" )"
if [ -n "$SSH_USER_CHECK" ]; then
SSH_USER="$( echo "$CONN_STRING" | cut -d"@" -f1 )"
SSH_HOST="$( echo "$CONN_STRING" | cut -d"@" -f2 )"
else
SSH_USER="$(whoami)"
SSH_HOST="$CONN_STRING"
fi
# Show parameters
echo "SSH user: $SSH_USER"
echo "SSH host: $SSH_HOST"
[ -n $SSH_OPTS ] && echo "SSH options: $SSH_OPTS"
echo
stop_and_fail() {
echo "Timeout after 10s"
exit 1
}
# Check SSH function
check_ssh () {
echo "Waiting for SSH connection"
START_WAIT="$( date +%s )"
while [ $( $NC -w 5 -z $SSH_HOST 22 > /dev/null ; echo $? ) -ne 0 ]; do
sleep 1
done
echo
STOP_WAIT="$( date +%s )"
TOTAL_WAIT="$( echo "scale=2; $STOP_WAIT - $START_WAIT" | bc )"
tput bel
tput bel
echo "SSH ready (waited for $TOTAL_WAIT seconds)"
echo
}
check_ssh