1

I'd like to get a remote file with SFTP, but choosing stdout as the local destination. I tried the obvious code below, but it failed:

echo 'get <Remote file> /dev/stdout' | sftp <User>@<Host>:
sftp> <Remote file> /dev/stdout   
Fetching <Remote file> to /dev/stdout 
<Remote file>                                               100%   28KB  57.6KB/s   00:00
ftruncate "/dev/stdout": Invalid argument
Couldn't write to "/dev/stdout": Illegal seek

The reason for that is that I want to download /etc/passwd and /etc/group to find numeric uids/gids for later chown and chgrp commands, in order to solve this problem. But it would be nicer if I could download such files to a variable, instead of a temporary local file.

A possibility would be to create a local named pipe, and save remote file to it, but I'd like to find a less complicated solution. An alternative would be an additional SSH session to get such data but, since we use timed tokens (TOTP), that would mean additional 30s per remote host...

SCP is forbidden in our network so, unfortunately, it's not an option.

Can SFTP get "save" to stdout?

Thanks!

2
  • 1
    Does this answer your question? Need a non-GUI SFTP tool better than "sftp". Something like echo 'cat <Remote file>' | lftp sftp://<User>@<Host> Mar 29, 2021 at 17:42
  • use lftp; it has a convenient cat command. I just tested it and echo cat somefile | lftp sftp://user@host works fine.
    – user1003916
    Apr 1, 2021 at 7:10

1 Answer 1

2

The way SFTP works, the download is not necessarily sequential. That's why sftp needs a seekable destination. So it does not work with /dev/stdout.

The client can serialize the data in memory, if the server sends it out of order. If the lftp suggested by @Kamil works, it must be doing that. My Windows WinSCP client does that too. But OpenSSH sftp does not.


But usually, if you can "sftp", you also can "scp". The SCP download is sequential, so this works:

scp -q [email protected]:/remote/path /dev/stdout

Or if you have at least partial shell access, you can also do:

ssh [email protected] cat /remote/path

Though note that since OpenSSH 9.0, the scp is actually SFTP client by default. To make it use SCP protocol, add -O switch.

0

Not the answer you're looking for? Browse other questions tagged .