2

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

1 Answer 1

2

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 […] 
6
  • thanks, examples for both would be great! I imagine the local end would be like tar cf - file1 file2 file3 ... | ssh ..., however I am not sure how to handle the files on the other end. tee? Commented Jun 11, 2011 at 19:58
  • Ok, I realized i left out something crucial, that the files should be piped as input to commands on the remote end! So I want to keep the data in the pipe and never extract to files! Apologies...! Commented Jun 11, 2011 at 20:15
  • I'd give you an example with a named pipe, but it's exactly the same as a regular pipe except that you set it up ahead of time then write to it when you're ready to. I can't see how this addresses the problem in your question without more details. Commented Jun 11, 2011 at 20:15
  • @grojo: This isn't making sense yet. How are you supposed to delineate between files? Does each file that gets sent run a new command on the remote end or can you just concatenate them all together and send them across? Commented Jun 11, 2011 at 20:16
  • You could use cat - | tar xO | command on the receiving end to pipe to a command instead of extracting to files, but in that case tar is wasted unless you wanted to compress along the wire because cat could have done the same thing. Commented Jun 11, 2011 at 20:26

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.