-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathressh
executable file
·59 lines (51 loc) · 1.61 KB
/
ressh
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
#!/usr/bin/env bash
################################################################################
# ressh - maintain a persistent SSH connection. Could be used with something
# like tmux or screen to have an automatically-attached terminal session, even
# while switching between networks/interfaces.
#
# Version 2
# Matthew Malensek <[email protected]>
################################################################################
# Default Options (set RESSH_OPTS to override)
# Close hung connections quickly:
ssh_options="${RESSH_OPTS:--oServerAliveInterval=1 -oServerAliveCountMax=3 -t}"
sleep_time=1
unset verbose
################################################################################
script_name=$(basename ${0})
print_usage() {
cat <<EOM
Usage: ${script_name} [options] [user@]hostname [ssh-options] command
-s <number> Seconds to sleep between connection retries. \
Default: ${sleep_time}
-v Verbose output; don't clear the screen
EOM
}
while getopts "s:v" flag; do
case ${flag} in
s) sleep_time=${OPTARG} ;;
v) verbose=1 ;;
?) print_usage; exit 1 ;;
esac
done
shift $(($OPTIND - 1))
# make sure we have at least 2 parameters
if [[ ${#} -lt 2 ]]; then
print_usage
exit 1
fi
destination=${1}
shift
while true; do
ssh ${ssh_options} ${destination} ${@}
# exit status is 255 if an error occurred; otherwise the remote command
# returned successfully.
if [[ ${?} -eq 255 ]]; then
[[ -n ${verbose} ]] || clear
echo "${script_name}: Connection Error! Retrying..."
sleep ${sleep_time}
else
break
fi
done