39

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

4 Answers 4

40

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

scp remotehost:/path/to/remote/file /dev/stdout
6
  • 1
    Neither method works. The first one is equivalent to the one-liner sftp username@hostname:/path/to/file.txt /dev/stdout and results in "Couldn't write to "/dev/stdout": Illegal seek". The second command fails, and shows the error that is shown at the bottom of my question.
    – Rob W
    Commented Jul 29, 2014 at 14:14
  • The SFTP form works fine for me. It may depend on what version of the ssh software you're using. Regarding scp, I did say "if scp works". You established in your question that the server was not permitting you to perform scp, so naturally the scp command would fail for you.
    – Kenster
    Commented Jul 29, 2014 at 17:06
  • ssh -V gives OpenSSH_6.6.1p1, OpenSSL 1.0.1h 5 Jun 2014. scp fails because it uses ssh under the hood, and ssh is disabled (as a security measure, see e.g. serverfault.com/questions/354615/allow-sftp-but-disallow-ssh)
    – Rob W
    Commented Jul 29, 2014 at 17:13
  • 1
    I get scp: local ftruncate "/dev/stdout": Invalid argument scp: write local "/dev/stdout": Illegal seek. I have openssh-client 1:9.6p1-4 (Debian). Commented Feb 27 at 13:41
  • 1
    @alx-recommendscodidact The "local ftruncate" error is emitted by the SFTP file transfer code when it tries to write to an existing file. Modern versions of scp can use sftp under the hood to do the file transfer, and can emit that error. Try using scp with the "-O" (capital-O) option to use the legacy scp protocol.
    – Kenster
    Commented Feb 27 at 20:09
19

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
2
  • Thanks, exactly what I was looking for! I have edited your answer to expand on public-key authentication, it turns out that the syntax is very similar to the ssh/sshfs syntax. If the curl command fails with "curl: (51) SSL peer certificate or SSH remote key was not OK", just add the -k flag (--insecure).
    – Rob W
    Commented Jul 28, 2014 at 21:46
  • #2 fails with no URL specified!, #3 fails with (67) Login denied. It works when entering password manually :( Commented Feb 22 at 15:21
0

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).

0

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).

You must log in to answer this question.

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