如何通过 scp 或 sftp 将远程文件通过管道传输到标准输出?

Rob*_*b W 34 ssh scp sftp cat

使用 ssh,可以很容易地使用打印文件的内容

ssh host 'cat file.txt'
Run Code Online (Sandbox Code Playgroud)

禁用 ssh 且仅启用 SFTP 时,运行上一个命令会出现以下错误:

此服务仅允许 sftp 连接。

为了解决这个问题,我可以使用scpor创建一个临时文件sshfs(如下所示),但这看起来真的很难看。禁用 SSH 时打印远程文件内容的正确方法是什么?

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
Run Code Online (Sandbox Code Playgroud)

Ken*_*ter 36

对于可以跑步的人scp,你可以这样做:

scp remotehost:/path/to/remote/file /dev/stdout
Run Code Online (Sandbox Code Playgroud)


小智 18

Curl 可以像 cat 一样显示文件。无需删除文件,因为它只是显示输出,除非您告诉它否则。

curl -u username:password sftp://hostname/path/to/file.txt
Run Code Online (Sandbox Code Playgroud)

如果您使用公钥认证:

curl -u username: --key ~/.ssh/id_rsa --pubkey sftp://hostname/path/to/file.txt
Run Code Online (Sandbox Code Playgroud)

如果使用默认位置,则--key--pubkey可以省略:

curl -u username: sftp://hostname/path/to/file.txt
Run Code Online (Sandbox Code Playgroud)

用户名也可以是 URL 的一部分,因此最终结果看起来非常接近 ssh 命令:

curl sftp://username@hostname/path/to/file.txt
Run Code Online (Sandbox Code Playgroud)