How to read a file via ssh / scp directly
I have a program written in C / C ++ that reads two files and then generates some reports. A typical workflow looks like this:
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 to allow my program to directly process the remote file without explicitly copying the file to the local one? I tried the following command but it doesn't work for me.
> my_program <(ssh user@server01:/temp/file1.txt) localfile.txt
source to share
You need to use ssh user@host "cat /temp/file.txt"
.
You can use this to redirect it to the stdin of your program: ssh user@host "cat /temp/file.txt" | my_program
or you can actually call it in your program with fork / exec. Or as you described it to create a temporary file:my_program <(ssh user@host "cat /temp/file.txt")
Other options might be using fused sshfs, or using the ssh (or VFS) library in your program.
source to share
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 /dev/stdin
may work as well, but it will depend on whether the my_program
argument was interpreted -
as "reading this file from stdin".
Also, it might not work if it my_program
expects a lookup in the first file (I / O streams are generally not searchable), but if all it does is read from the first byte to the last byte, it should be OK ...
source to share