Linux – pipe multiple files, single ssh connection, remote commands

freebsdlinuxpipessh

I'd like to pipe several files to a remote server, piped as input to commands run on the remote server, over ssh, but only one ssh connection/command is permitted. (In the remote authorized_keys file, "command=…" is specified, so only one single command/script can be run on the remote end using that key).

Is it possible to have a setup where multiple files go in the same pipe – I've been considering tar'ing them and having some logic in the remote script/untar to separate the files to separate commands

The initial setup was something like

cat file1 | ssh -i identity remote.host 'remote command1 <'
cat file2 | ssh -i identity remote.host 'remote command2 <'
...etc

Using tar, it could be something like

tar cf - file1 file2 file3 | ssh -i identity remote.host 'remote command.sh <'

The remote script would need to be able to extract the files one by one and pipe the data into separate commands, but I am not sure how to achieve this

Best Answer

If the boundaries between files don't mater, just cat file1 file2 | ssh …. If they do, use tar. This is actually very close to what it was invented for and precisely what it does.

The other option would be to setup a named pipe that would keep the ssh connection open with a file node for it's input. You could keep dumping things into at will.

Edit: Example usage for tar:

On the receiver end inside the script that gets run as remote command move to where you want to files to be extracted to and then read 'em in:

#!/bin/bash
cd /path/to/extract/destination
cat - | tar x

And on the sending side:

tar c file1 file1 | ssh […]