Command file command Hide password

I have this batch file that I wrote to open putty and want to make it a universal script for others. the script is as follows

@echo off
::Written by Mark Gulick::
::Today 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

I would like the password not to be displayed and try some areas here, and not find one that will work. Maybe I'm wrong too. I'm a little rusty on my scripts. Am I missing something or should I use something other than the set command?

+3
source share
3 answers

You can do something like this:

@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     
::***********************************
+3
source

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 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
+4

batch-file , , , , , , VBScript. , :


randomThing.bat

echo Blah blah blah...
:: Call a vbscript file with the outputs being set as a variable
for /f "usebackq tokens=*" %%r in (`wscript "password.vbs"`) do set retPass=%%r
:: Write back the input for example sake.
echo What you typed in: %retPass%

password.vbs

'Call this script from a batch file to input a masked password
set objPassword = createObject("scriptPW.password") 
wScript.stdOut.write "Input Password:" 
strPassword = objPassword.getPassword() 
wScript.echo strPassword

Note. I answer this on a mobile phone and as such cannot check if this method works. However, he gets the main idea.

0
source

All Articles