Search code examples
linuxwindowssshopenssh

ssh from Linux to Windows - why need so many slashes in path?


I am trying to run commands over ssh from Linux machine to Windows machine.

Windows machine have OpenSSHx64 installed

Following command with double quotes fails:

ssh user@win8-pc "ls -l \\\\172.21.15.120\\vol0slash"

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

The same command with single quotes still fails, but at least display single slash:

ssh user@win8-pc 'ls -l \\\\172.21.15.120\\vol0slash'
ls: cannot access \172.21.15.120vol0slash: No such file or directory

Surrounding path with single quotes almost works, but still one root slash is missing:

ssh user@win8-pc "ls -l '\\\\172.21.15.120\\vol0slash'"
ls: cannot access \172.21.15.120\vol0slash: No such file or directory

Now finally adding fifth slash to UNC path root, did the trick:

ssh user@win8-pc "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

Can anyone explain the logic behind this behavior?


Solution

  • Backslash is special symbol in bash and quite much in all Linux shells, therefore if you need to use it, it has to be escaped using another \ (backslash). The command is passed through the remote bash such as

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

    which transfers evaluates the special characters and makes it look like

    ls -l '\\\172.21.15.120\vol0slash'
    

    when it is supposed to run.

    The problem with using odd number of backslashes will end up evaluating as a special character, so you should go with even number, if you want to see a backslash in the end.

    The other thing is how the arguments are interpreted by ls on Windows (which I have no idea about). See the tests with simple echo:

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

    Similarly you can explain the original command without ':

    ssh user@win8-pc "ls -l \\\\172.21.15.120\\vol0slash"
    

    gets already in local shell (because it is not in ')

    ssh user@win8-pc "ls -l \\172.21.15.120\vol0slash"
    

    and remote shell gets already

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

    which evaluates to

    bash -c "ls -l \172.21.15.120vol0slash"
    

    and to

    ls -l 172.21.15.120vol0slash
    

    which is obviously not existing.