Ssh – Passing multiple files through stdin (over ssh)

io-redirectionssh

I have a program on a remote host, whose execution I need to automate. The command execute that program, on the same machine, looks something like this:

/path/to/program -a file1.txt -b file2.txt

In this case, file1.txt and file2.txt are used for entirely different things within the program, so I can't just cat them together. However, in my case, the file1.txt and file2.txt that I want to pass into the program exist only on my device, not on the host where I need to execute the program. I know that I can feed at least one file through SSH by passing it through stdin:

cat file1.txt | ssh host.name /path/to/program -a /dev/stdin -b file2.txt

but, since I'm not allowed to store files on the host, I need a way to get the file2.txt over there as well. I'm thinking it might be possible through abuse of environment variables and creative use of cat and sed together, but I don't know the tools well enough to understand how I would use them to accomplish this. Is it doable, and how?

Best Answer

If the files given as arguments to your program are text files, and you're able to control their content (you know a line which doesn't occur inside them), you can use multiple here-documents:

{
    echo "cat /dev/fd/3 3<<'EOT' /dev/fd/4 4<<'EOT' /dev/fd/5 5<<'EOT'"
    cat file1
    echo EOT
    cat file2
    echo EOT
    cat file3
    echo EOT
} | ssh user@host sh

Here cat is a sample command which takes filenames as arguments. It could be instead:

echo "/path/to/prog -a /dev/fd/3 3<<'EOT' -b /dev/fd/4 4<<'EOT'

Replace each EOT with something that doesn't occur in each of the files, respectively.

Related Question