10

I have a program written in C/C++ that reads two files and then generate some reports. The typical workflow is as follows:

1> scp user@server01:/temp/file1.txt ~/ then input my password for the prompty

2> my_program file1.txt localfile.txt

Is there a way that I can let my program directly handle the remote file without explicitly copying the file to local first? I have tried the following command but it doesn't work for me.

> my_program <(ssh user@server01:/temp/file1.txt) localfile.txt
q0987
  • 34,938
  • 69
  • 242
  • 387

2 Answers2

15

You need to use ssh user@host "cat /temp/file.txt".

You can use this to redirect it to stdin of your program: ssh user@host "cat /temp/file.txt" | my_program or you can actually call this in your program with an fork/exec. Or like you described it to generate a temporary file: my_program <(ssh user@host "cat /temp/file.txt")

Other options would be to use sshfs with fuse or using a ssh (or VFS) library in your program.

eckes
  • 10,103
  • 1
  • 59
  • 71
  • I tried this solution and it doesn't work. my_program only reads the localfile.txt but the remote file. – q0987 Dec 15 '14 at 19:37
  • 1
    Your program needs to read from stdin for my example. Some do that automatically if you specify no input file, some do it if you specify '-' as filename.If your program does not do that, then you need to use the temp file function you suggested in your question, i adjust the answer. The main paint of my answer is using "cat". – eckes Dec 15 '14 at 19:43
  • 2
    also you will probably need to setup passwordless ssh using authorized keys else you are going to have you program send your pssword – Doon Dec 15 '14 at 19:46
6

If you are really on Red Hat Linux, this should work:

ssh user@host cat /temp/file1.txt | my_program /dev/stdin localfile.txt

Using a single - instead of /dev/stdin may work as well, but it would depend on whether my_program was written to interpret an argument of - as "read this file from stdin".

Also, this might not work if my_program expects to be able to seek around in the first file (I/O streams are generally non-seekable), but if all it does is read from the first byte to the last byte, it should be OK.

twalberg
  • 59,951
  • 11
  • 89
  • 84