2017-01-23 60 views
0

我想通過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 

任何人都可以解釋這種行爲背後邏輯?

回答

1

反斜槓是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 

這顯然是不存在的。

相關問題