我使用的是Windows11 Pro版本: 21H2 (构建: 22000.978)
因此,我的设备上有OpenSSH,也就是将添加到系统路径变量中,如下面的命令提示符片段所示。
C:\Windows\System32>set path
Path=C:\oraclexe\app\oracle\product\11.
\VMware Workstation\bin\;C:\WINDOWS\sys
1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Pr
\ProgramData\nvm;C:\Program Files\nodej
m Files\dotnet\;C:\Android\android-sdk\
in;C:\Program Files\Git\cmd;C:\Program
我在System32文件夹中,在命令提示符提示符(normal window \xnon)中,通过输入以下命令打开当前目录E 219
中的Explorer:
explorer .
确实看到了OpenSSH文件夹,但,当我试图进入(更改目录)到OpenSSH时,命令提示符抛出一个错误,表示路径不存在E 230
。但它确实存在于硬盘上!
C:\Windows\System32>cd OpenSSH
The system cannot find the path specified.
显然,我无法访问位于ssh.exe
目录中的OpenSSH目录。
但是interestingly,当我提升shell (将CMD打开为管理员)并尝试访问OpenSSH目录甚至文件ssh.exe
时,工作了!当我使用where
命令 for ssh
时,它指向正确的目录,如下所示:
命令提示符作为标准用户:
C:\Windows\System32>ssh
'ssh' is not recognized as an internal or external command,
operable program or batch file.
命令提示符作为管理员:
C:\Users\Admin>where ssh
C:\Windows\System32\OpenSSH\ssh.exe
它只是在普通窗口中对cmd不工作,但是当cmd作为打开时,工作。
发布于 2022-09-19 06:42:33
请首先阅读有关Windows 文件系统重定向器的微软文档,以及最好的文档页WOW64实现细节和受WOW64影响的注册表项。
当64位Windows上的32位应用程序使用%SystemRoot%\System32\cmd.exe
或%ComSpec%
或cmd.exe
启动%SystemRoot%\System32\cmd.exe
时,或者在最坏的情况下仅用cmd
启动32位应用程序时,由于文件系统重定向器,%SystemRoot%\SysWOW64
目录中会启动32位版本的%SystemRoot%\SysWOW64
命令处理器。
文件系统重定向器还负责将%SystemRoot%\System32\OpenSSH
的每次访问重定向到64位Windows上不存在的%SystemRoot%\SysWOW64\OpenSSH
,因为OpenSSH包只有在64位Windows上的64位应用程序套件可用时才可用(取决于OpenSSH的版本)。
作为管理员的使用会在64位Windows上启动%SystemRoot%\System32
中的64位cmd.exe
,因此对%SystemRoot%\System32
中的文件和目录的任何访问都不会进行文件系统重定向。有关详细信息,请参阅为什么“以管理员身份运行”更改(有时)批处理文件的当前目录?
在Windows批处理文件中,可以使用以下代码:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Expect ssh.exe in OpenSSH in Windows system directory by default.
set "ExeSSH=%SystemRoot%\System32\OpenSSH\ssh.exe"
if exist "%ExeSSH%" goto RunSSH
rem Expect ssh.exe in OpenSSH in native Windows system directory
rem on batch file processed by 32-bit cmd.exe on 64-bit Windows.
set "ExeSSH=%SystemRoot%\Sysnative\OpenSSH\ssh.exe"
if exist "%ExeSSH%" goto RunSSH
rem Search for OpenSSH using environment variable PATH.
rem The environment variable ExeSSH is undefined by the
rem next command line on no file ssh.exe found by cmd.exe.
for %%I in (ssh.exe) do set "ExeSSH=%%~$PATH:I"
if defined ExeSSH goto RunSSH
rem There could not be found the executable ssh.exe anywhere.
echo ERROR: Could not find ssh.exe in any directory.
echo/
pause
exit /B 1
:RunSSH
rem Use here "%ExeSSH%" ... to run this executable.
echo Found: "%ExeSSH%"
endlocal
此批处理文件可用于Windows和所有较新的Windows版本,因此,在旧版本上,最有可能输出的是由于默认情况下未安装可执行文件ssh.exe
而找不到的错误消息。
https://stackoverflow.com/questions/73774215
复制