-
Notifications
You must be signed in to change notification settings - Fork 19
/
deliver_ssh_wrapper.sh
executable file
·57 lines (47 loc) · 1.9 KB
/
deliver_ssh_wrapper.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
#!/bin/bash
#
# Copyright 2012-2015 Arnaud Betremieux <arno@arnoo.net>
#
# This file is a part of Git-deliver.
#
# Adapted with authorization from Ross Patterson's SSH wrapper
# (http://ratterson.net/blog/re-using-and-multiplexing-ssh)
#
# The program in this file is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
## This is a Wrapper script for ssh that creates a control master
## in the background if one isn't already started.
master_id=$1
shift
if [[ $# == 0 ]]; then
ssh -q -S ${master_id} -O exit not_used &> /dev/null
exit
fi
## OpenSSH as included in the current msys or cygwin installs does not support
## multiplexing with privilege separation -> forget it
if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]]; then
exec ssh "$@"
fi
## optstring assembled from `man ssh`
optstring="+1246AaCfgKkMNnqsTtVvXxYb:c:D:e:F:i:L:l:m:O:o:p:R:S:w:z"
## get ssh options
while getopts "$optstring" optchar; do
true
done
## the host is the first non-option arg
host="${!OPTIND}"
## if the master isn't running, start it in the background
ssh -S $master_id -q -O check not_used 2>/dev/null || { SSH_OPTION=`ssh -V 2>&1 | awk 'BEGIN {FS="_"} $2>=5.6 { print "-o ControlPersist=5m"}'` ; ssh $SSH_OPTION -S $master_id -MNf $host > /dev/null || exit 255; }
## replace ourselves with the reall ssh call
exec ssh -S $master_id "$@"