While you can do this inside a single ssh session, it's a bit tricky to combine copying files with running commands.
The easiest way to tackle this task is to run separate SSH sessions for the three operations:
rsync -a inputs/ machineB:inputs/
ssh machineB 'some command -i inputs -o outputs'
rsync -a machineB:outputs/ outputs/
This requires authenticating to machineB three times. The recommended way to avoid authenticating multiple times is to use the connection sharing facility in modern versions of OpenSSH: start a master connection to B once and for all, and let SSH automatically piggyback onto that master connection. Add ControlMaster auto
and a ControlPath
line to your ~/.ssh/config
, then start a master connection in the background, then perform your tasks.
ssh -fN machineB # start a master connection in the background
# Subsequent connections will be slaves to the existing master connection
rsync -a inputs/ machineB:inputs/
ssh machineB 'some command -i inputs -o outputs'
rsync -a machineB:outputs/ outputs/
Rather than use scp or rsync to copy files, it may be easier to mount the remote filesystem under SSHFS. This will take care of setting up a master connection, by the way (assuming you've set up your ~/.ssh/config
as indicated above).
mkdir /net/machineB
sshfs machineB: /net/machineB
cp -Rp inputs /net/machineB/
ssh machibeB 'some command -i inputs -o outputs'
cp -Rp /net/machineB/outputs .
cat file | ssh user@host 'cat > /destination/of/file; /path/to/script &>/dev/null; cat results' > /destination/of/results
– phemmer Mar 11 '12 at 23:04ControlMaster=yes
andControlPath=/path/to/socketfile
, and then start one ssh connection with-f
to run a background ssh. Tell all subsequent SSH connections to use the same socket file. – jsbillings Mar 11 '12 at 23:06