从Linux到Windows的ssh - 为什么需要这么多的斜杠?

问题描述:

我想通过ssh从Linux机器到Windows机器运行命令。从Linux到Windows的ssh - 为什么需要这么多的斜杠?

Windows机器已经OpenSSHx64安装

在用双引号命令失败:

ssh [email protected] "ls -l \\\\172.21.15.120\\vol0slash" 

ls: cannot access 172.21.15.120vol0slash: No such file or directory 

用单引号同样的命令仍然失败,但至少显示单斜杠:

ssh [email protected] 'ls -l \\\\172.21.15.120\\vol0slash' 
ls: cannot access \172.21.15.120vol0slash: No such file or directory 

使用单引号的环绕路径几乎可行,但仍缺少一个根斜杠:

ssh [email protected] "ls -l '\\\\172.21.15.120\\vol0slash'" 
ls: cannot access \172.21.15.120\vol0slash: No such file or directory 

现在终于加入第五斜杠UNC路径根,没有诀窍:

ssh [email protected] "ls -l '\\\\\172.21.15.120\\vol0slash'" 
total 536 
drwxr-xr-x 1 Admin Domain Users 0 Jan 23 08:33 GeneralSystemDiagnostic 
drwxr-xr-x 1 Admin Domain Users 0 Jan 22 08:10 cifs 
-rw-r--r-- 1 Admin Domain Users 336 Jan 23 12:00 linux.txt 
drwxr-xr-x 1 Admin Domain Users 0 Jan 19 14:11 nfs 

任何人都可以解释这种行为背后逻辑?

反斜杠是bash中的特殊符号,并且在所有Linux shell中都很多,所以如果需要使用它,必须使用另一个\(反斜杠)将其转义。该命令是passed through the remote bash

bash -c "ls -l '\\\\\172.21.15.120\\vol0slash'" 

其传输评估特殊字符,使它看起来像

ls -l '\\\172.21.15.120\vol0slash' 

当它应该运行。

使用奇数个反斜杠的问题最终将作为特殊字符进行评估,所以如果您想在最后看到反斜杠,则应该使用偶数。

另一件事是如何在Windows上解析参数ls(我不知道)。见测试用简单的echo

$ ssh f25 "echo '\1'" 
\1 
$ ssh f25 "echo '\\1'" 
\1 
$ ssh f25 "echo '\\\1'" 
\\1 
$ ssh f25 "echo '\\\\1'" 
\\1 

同样可以不用'解释原始命令:

ssh [email protected] "ls -l \\\\172.21.15.120\\vol0slash" 

在当地壳已经得到(因为它不是在'

ssh [email protected] "ls -l \\172.21.15.120\vol0slash" 

和远程外壳已获得

bash -c "ls -l \\172.21.15.120\vol0slash" 

计算结果为

bash -c "ls -l \172.21.15.120vol0slash" 

,并

ls -l 172.21.15.120vol0slash 

这显然是不存在的。