-
Notifications
You must be signed in to change notification settings - Fork 2
/
ssh-jump.sh
executable file
·145 lines (124 loc) · 3.41 KB
/
ssh-jump.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
_sdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
show_help(){
cat <<HELP
$(basename $0) [options] -t PORT [-u USER] [-- [commands]]
Reconnects if connection drops. (Requires Tmux on the remote side.)
Options:
-t TARGET_PORT : Target's SSHD port on rendezvous server.
-u USER : Username on the target. Defaults to \$USER
-k, --known-hosts FILE : Path to known_hosts file.
-n, --no-reconnect : Do not try to reconnect
-- [commands] : Use custom commands instead of a Tmux session.
-c, --config FILE : Alternative config file (default: ./config.sh)
HELP
}
die(){
echo
echo "$@"
show_help
exit 1
}
SSH_OPTS="-o ServerAliveInterval=10 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes -o AddressFamily=inet"
# Parse command line arguments
# ---------------------------
# Initialize parameters
target_user=$USER
target_port=
reconnect=true
no_reconnect=false
known_hosts_file=
cmd=()
default_cmd=true
config="$_sdir/config.sh"
# ---------------------------
args_backup=("$@")
args=()
_count=1
while [ $# -gt 0 ]; do
key="${1:-}"
case $key in
-h|-\?|--help|'')
show_help # Display a usage synopsis.
exit
;;
# --------------------------------------------------------
-u) shift
target_user="$1"
;;
-t) shift
target_port="$1"
;;
--) shift
cmd=("$@")
reconnect=false
default_cmd=false
break
;;
-n|--no-reconnect)
no_reconnect=true
;;
-k|--known-hosts) shift
known_hosts_file=$1
;;
-c|--config) shift
config="$1"
;;
# --------------------------------------------------------
-*) # Handle unrecognized options
die "Unknown option: $1"
;;
*) # Generate the new positional arguments: $arg1, $arg2, ... and ${args[@]}
if [[ ! -z ${1:-} ]]; then
declare arg$((_count++))="$1"
args+=("$1")
fi
;;
esac
shift
[[ -z ${1:-} ]] && break
done; set -- "${args_backup[@]}"
# Use $arg1 in place of $1, $arg2 in place of $2 and so on,
# "$@" is in the original state,
# use ${args[@]} for new positional arguments
[[ -f "$config" ]] || die "No configuration found."
source "$config"
[[ -z "$target_user" ]] && die "Target user missing."
[[ -z "$target_port" ]] && die "Target port missing."
[[ -n $known_hosts_file ]] \
&& UserKnownHostsFile="UserKnownHostsFile $known_hosts_file" \
|| UserKnownHostsFile=""
tmpfile=$(mktemp /tmp/$(basename $0).XXXXXX)
cleanup(){
rm $tmpfile
}
trap cleanup EXIT
ssh_config=`cat << EOF > $tmpfile
Host *
$UserKnownHostsFile
IdentityFile $SSH_KEY_FILE
Host LINK_UP_SERVER
Hostname $SSH_HOST
Port $SSH_PORT
User $SSH_USER
Host target
Hostname localhost
Port $target_port
User $target_user
ProxyJump LINK_UP_SERVER
EOF
`
ssh_jump(){
ssh $SSH_OPTS -F $tmpfile target "$@"
}
if $default_cmd; then
cmd+=("-t")
cmd+=('tmux a || tmux; bash --login')
fi
period=0
while sleep $period; do
ssh_jump ${cmd[@]} && break
$no_reconnect && break
period=1
echo "Reconnecting in ${period}s."
done