使用 scp 将文件复制到不存在的本地目录 - 误导性错误消息

vst*_*der 2 linux bash scp openssh

我正在编写一个 shell 脚本,用于使用scp. 如果目标目录不存在,我会收到误导性错误消息。IE:

scp user@remote-host:/path/to/existing/file /local/non-existing/directory/
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

/local/non-existing/directory/: Is a directory
Run Code Online (Sandbox Code Playgroud)

这是令人费解的......我希望是这样的:

Directory /local/non-existing/directory/ does not exist
Run Code Online (Sandbox Code Playgroud)
  1. 它对任何人有意义还是只有我有意义?
  2. 你认为这是scp的缺陷吗?
  3. 我正在运行 Ubuntu 14.04.3 LTS,在其他操作系统中是否相同?

Eri*_*nil 5

漏洞

找到了相关的bug。它似乎已在 Ubuntu 14.04.5 (openssh 1:6.6p1-2ubuntu2.8) 中修复

Redhat 错误报告中有一个解释:

这个问题也存在于原始的 openSSH 中,当您尝试将某些内容复制到具有现有父目录的不存在目录中时会出现此问题。缺少对该边界条件的检查。当 scp 进程尝试写入上述文件时会抛出错误消息,但该文件以斜杠结尾(这是对目录的解释),这就是当前错误消息的原因。

原答案

无法重现(在 Ubuntu 14.04.5 LTS 和 Linux Mint 17 上测试)

故障排除

ssh user@remote-host "file /path/to/existing/file"
Run Code Online (Sandbox Code Playgroud)

应该为您提供有关远程文件(或可能的目录)的信息。

file /local/non-existing/directory/
Run Code Online (Sandbox Code Playgroud)

应该为您提供有关本地目录的信息

mkdir -p /local/non-existing/directory/
Run Code Online (Sandbox Code Playgroud)

将递归创建目录,并在必要时创建任何父目录。

之后mkdir -p,如果file真的是一个文件并且/local/non-existing/directory/真的是一个目录,那么您的scp命令应该可以工作。