Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read file through ssh/scp directly

Tags:

linux

redhat

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
like image 811
q0987 Avatar asked Dec 15 '14 19:12

q0987


People also ask

Does SCP work over SSH?

The scp command uses SSH to transfer data, so it requires a password or passphrase for authentication. Unlike rcp or FTP, scp encrypts both the file and any passwords exchanged so that anyone snooping on the network cannot view them.

Can I copy file through SSH?

Copying files via SSH uses the SCP (Secure Copy) protocol. SCP is a method of securely transferring files and entire folders between computers and it is based on the SSH protocol that it's used with. Using SCP a client can send (upload) files securely to a remote server or request (download) files.


2 Answers

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.

like image 88
twalberg Avatar answered Oct 05 '22 23:10

twalberg


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.

like image 45
eckes Avatar answered Oct 05 '22 23:10

eckes