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!

1 Answers1

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 user@example.com:/remote/path /dev/stdout

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

ssh user@example.com 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.