批处理文件命令隐藏密码

Mar*_*ick 8 batch-file hide

我有这个批处理文件,我写的是打开putty,并希望将其作为其他人的通用脚本.脚本如下

@echo off
::Written by Mark Gulick::
::Today's Date 20150316::

set /p U="Enter Username: "
set /p P="Enter Password: "
set /p DC="Enter DC Number: "
start /d "C:\Program Files (x86)\putty\" PUTTY.EXE %U%@b0%DC%db -pw %P%
pause
Run Code Online (Sandbox Code Playgroud)

我想让密码不显示,并在这里尝试了一些区域,但没有找到一个可以工作的区域.我也可能做错了.我的脚本上有点生疏了.我错过了什么或者我应该使用set命令以外的其他东西吗?

Som*_*ark 6

这篇关于DOSTips的帖子引用了MC ND的帖子,但我找不到原文,所以这里又是.每当您想要获取密码并屏蔽输入时,只需将您存储密码的变量的名称call :getPassword target_variable input_prompt放在哪里target_variable,并且input_prompt是您向用户显示的任何内容,以提示他们输入密码.

@echo off
set /p "user_name=Enter username here:"
call :getPassword user_password "Enter password here: "
:: The user's password has been stored in the variable %user_password%

exit /b

::------------------------------------------------------------------------------
:: Masks user input and returns the input as a variable.
:: Password-masking code based on http://www.dostips.com/forum/viewtopic.php?p=33538#p33538
::
:: Arguments: %1 - the variable to store the password in
::            %2 - the prompt to display when receiving input
::------------------------------------------------------------------------------
:getPassword
set "_password="

:: We need a backspace to handle character removal
for /f %%a in ('"prompt;$H&for %%b in (0) do rem"') do set "BS=%%a"

:: Prompt the user 
set /p "=%~2" <nul 

:keyLoop
:: Retrieve a keypress
set "key="
for /f "delims=" %%a in ('xcopy /l /w "%~f0" "%~f0" 2^>nul') do if not defined key set "key=%%a"
set "key=%key:~-1%"

:: If No keypress (enter), then exit
:: If backspace, remove character from password and console
:: Otherwise, add a character to password and go ask for next one
if defined key (
    if "%key%"=="%BS%" (
        if defined _password (
            set "_password=%_password:~0,-1%"
            set /p "=!BS! !BS!"<nul
        )
    ) else (
        set "_password=%_password%%key%"
        set /p "="<nul
    )
    goto :keyLoop
)
echo/

:: Return password to caller
set "%~1=%_password%"
goto :eof
Run Code Online (Sandbox Code Playgroud)


Hac*_*koo 5

您可以执行以下操作:

@echo off & setlocal DisableDelayedExpansion
Title %~n0
Mode 50,5 & Color 0E
set /p U="Enter Username : "
Call:InputPassword "Enter Password" P
set /p DC="Enter DC Number: "
setlocal EnableDelayedExpansion
start /d "C:\Program Files (x86)\putty\" PUTTY.EXE !U!@b0!DC!db -pw !P!
pause
::***********************************
:InputPassword
Cls
echo.
echo.
set "psCommand=powershell -Command "$pword = read-host '%1' -AsSecureString ; ^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
      [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
        for /f "usebackq delims=" %%p in (`%psCommand%`) do set %2=%%p
)
goto :eof     
::***********************************
Run Code Online (Sandbox Code Playgroud)

  • 谢谢 Hackoo,它按照我想要的方式运行得很好!我喜欢你添加的额外耀斑。 (2认同)
  • 我必须添加“ eol =“以允许特殊情况,如果密码包含一个分号“ / f”,则usebackq delims = eol =”(%psCommand%`)中的%% p必须设置%2 = %% p (2认同)