3 ### SSH wrapper to spawn separate SSH master connections on demand
5 ### (c) 2010 Mark Wooding
8 ###----- Licensing notice ---------------------------------------------------
10 ### This program is free software; you can redistribute it and/or modify
11 ### it under the terms of the GNU General Public License as published by
12 ### the Free Software Foundation; either version 2 of the License, or
13 ### (at your option) any later version.
15 ### This program is distributed in the hope that it will be useful,
16 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ### GNU General Public License for more details.
20 ### You should have received a copy of the GNU General Public License
21 ### along with this program; if not, write to the Free Software Foundation,
22 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 ###--------------------------------------------------------------------------
27 : ${REAL_SSH=/usr/bin/ssh}
29 ###--------------------------------------------------------------------------
30 ### Parse the command line and dredge out information.
32 ## This is a reasonable approximation to SSH's command-line argument parser.
35 declare -a opts masteropts
39 ## Nothing left. We're done.
44 ## A command line option group. Parse it and work out what's going on.
47 opts=("${opts[@]}" "$opt")
50 ## Strip off the first character, because it's the one we dealt with
55 ## Phase 1: identify the option and whether it needs an argument.
59 ## Empty group. We're done.
64 ## Options with arguments.
68 ## You're going to lose because there's no argument. But we'll
69 ## let SSH deal with that.
74 ## There's an argument cuddled on to the end of the option. It
75 ## will have been committed to the `opts' array as part of the
82 ## Nope. There's an argument in the next word. Grab it and
86 opts=("${opts[@]}" "$arg")
92 ## Anything else. Let it go even if it's not valid: SSH will moan
93 ## if it wants. Note that `--' is ignored by SSH, but this isn't
94 ## documented; in particular, `--' is /not/ a POSIX end-of-options
95 ## marker, so we don't try to handle it specially here either.
100 ## Phase two. Figure out whether what this means for us.
103 ## `-O foo' and `-S foo' mean that the caller wants to take
104 ## control of the multiplexing process.
109 ## Catch the login name if there is one. Make sure the master
112 masteropts=("${masteropts[@]}" "-l$arg")
116 ## These options are interesting to the master connection.
118 masteropts=("${masteropts[@]}" "-${o:0:1}$arg")
121 ## SSH options on the command line merit special attention. Pass
122 ## them onto the master, if necessary.
124 masteropts=("${masteropts[@]}" "${opt:0:1}$arg")
127 ## User wants to control the process. Let him.
128 ?,ControlMaster=* | ?,ControlPath=*)
138 ## A bare word. Maybe it's the hostname, or the start of the command.
149 ###--------------------------------------------------------------------------
150 ### Now to actually do the job.
152 ## If there's no host, pass straight through. We can't do anything useful
159 ## Actually do something useful.
162 exec "$REAL_SSH" "${opts[@]}" ${host+"$host"} "$@"
165 if ! "$REAL_SSH" -Ocheck ${login+"$login@"}"$host" >/dev/null 2>&1; then
166 "$REAL_SSH" -MNf "${masteropts[@]}" "$host"
168 exec "$REAL_SSH" ${opts[@]} "$host" "$@"
172 ###----- That's all, folks --------------------------------------------------