How to pipe a remote file to stdout over scp or sftp?

Using ssh, it is easy to print the contents of a file using

ssh host 'cat file.txt'

When ssh is disabled, and only SFTP is enabled, running the previous command gives the following error:

This service allows sftp connections only.

To work-around this issue, I could create a temporary file using scp or sshfs (as shown below), but that looks really ugly. What is the proper way to print the contents of a remote file when SSH is disabled?

mkdir tmpdir
sshfs host: tmpdir
cat tmpdir/file.txt
fusermount -u tmpdir

# This does not work! scp -v host:file.txt . shows
# "Sink: This service allows sftp connections only."
scp host:file.txt .
cat file.txt
rm file.txt
Asked By: Rob W

||

Curl can display the file the same way cat would. No need to delete the file since it simply displayed the output unless you tell it to do otherwise.

curl -u username:password sftp://hostname/path/to/file.txt

If you use public key authentication:

curl -u username: --key ~/.ssh/id_rsa --pubkey sftp://hostname/path/to/file.txt

If you use the default locations, then --key and --pubkey can be omitted:

curl -u username: sftp://hostname/path/to/file.txt

The user name can also be a part of the URL, so the final result looks very close to the ssh command:

curl sftp://username@hostname/path/to/file.txt
Answered By: brwtx

For people who can run scp, you can do this:

scp remotehost:/path/to/remote/file /dev/stdout
Answered By: Kenster

On Ubuntu I was able to open the remote directory in Files GUI, then "Open in Local Terminal"

At this point I was navigated to a funny path looking like /run/user/1000/gvfs/sftp:host=qweqwe.synology.me/Prod_Backup/DB

From there I could read and pipe files similar to local file systems.

Unfortunately the speed was not adequate for my needs (~200 KB/s).

Answered By: Anton Duzenko

With lftp:

lftp -e 'set cmd:show-status 0; cat -b remote/file' sftp://user@host < /dev/null

(</dev/null to force it to be non-interactive).

Answered By: Stéphane Chazelas
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.