18

My .bat file command to get user input from command line is

set /p weblogicpassword=Enter weblogic password:%=%

My .sh file command to get user input from bash script is

echo -n "Enter weblogic password: "
read weblogicpassword

Now when we enter some values for password, those values are visible in command line. How we can get the password values from command line which should be invisible to users like **

Mark
  • 3,609
  • 1
  • 22
  • 33
Obulesu Bukkana
  • 995
  • 4
  • 13
  • 25

4 Answers4

15

By using powershell, we can achieve this in 32 as well as 64 bit.

 @ECHO OFF
 set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
      $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
            [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
 for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p
 echo %password%

By using this we can get password with *** in command line

In bash script we can achieve this by using below code.

#!/bin/bash
prompt="Enter Password:"
while IFS= read -p "$prompt" -r -s -n 1 char 
do
if [[ $char == $'\0' ]];     then
    break
fi
if [[ $char == $'\177' ]];  then
    prompt=$'\b \b'
    password="${password%?}"
else
    prompt='*'
    password+="$char"
fi
done
echo " "
echo "Done. Password=$password" 

The options of the read command are: -p : Prompt string. -r : Don't use backslash as escape character. -s : Silent mode, inputs are not echoed. -n 1 : Number of character to input.

read returns 0 unless \0 is encountered, and the character the user types is placed into the char variable.

The IFS= part clears the IFS variable, which ensures that any space or tab characters that you type are included in the password rather than being parsed out by read.

Obulesu Bukkana
  • 995
  • 4
  • 13
  • 25
  • You'll want to add an additional check that the password is not empty before deleting characters otherwise you'll delete the actual prompt. – Nathan Jul 03 '15 at 11:48
  • How would you run this script by calling powershell in the batch version and then check if there was something entered or not? – rockower Jan 07 '19 at 15:44
14

For bash its read -s.

-s Silent mode. If input is coming from a terminal, characters are not echoed.

For batch it seems to be more complicated.

Read about it here: Can I mask an input text in a bat file

Community
  • 1
  • 1
RedX
  • 14,749
  • 1
  • 53
  • 76
  • And an implementation of this method could look like this: `a=''; while read -n 1 -s c; do [ "$c" = '' ] && break; echo -n '*'; a="$a$c"; done`. This doesn't allow the use of backspace, though. – Alfe Nov 13 '13 at 10:41
  • 2
    With backspace feature: `a=''; while read -n 1 -s c; do [ "$c" = '' ] && break; if [ "$c" = $'\x7f' ]; then [ "$a" != "" ] && { a=${a:0:-1}; printf "\b \b"; }; else echo -n '*'; a="$a$c"; fi; done;` – Alfe Nov 13 '13 at 10:47
  • now i'm getting *** in place of characters in bash script using your command.will u help me same in bat file also. – Obulesu Bukkana Nov 13 '13 at 11:34
8

Here is a solution for Windows 32 bit.

@echo off
echo hP1X500P[PZBBBfh#b##fXf-V@`$fPf]f3/f1/5++u5>%temp%\ftp.com
set /p password=What is your password? <nul
for /f "tokens=*" %%i in ('%temp%\ftp.com') do set "password=%%i"
del %temp%\ftp.com
echo password is "%password%"
pause
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • What is that odd string? `hP1X500P[PZBBBfh#b##fXf-V@`$fPf]f3/f1/5++u5`? – RedX Nov 14 '13 at 10:05
  • 1
    It is an Ascii Binary by Herbert Kleebauer. You will find many created by him in Usenet posts. – foxidrive Nov 14 '13 at 10:07
  • See http://social.technet.microsoft.com/Forums/scriptcenter/en-US/7752db4f-72ce-4538-8963-bf8f7676b45d/masking-password-through-cmd-file?forum=ITCG this for a 64 bit alternative. – RedX Nov 14 '13 at 10:07
0

I read all answers and I fixed one.

This code asks user for password. You can change password in if "%password%"=="SuperUser".

It checks input and if input is valid (SuperUser), it goes to label 1.

:1
echo True
cls
exit
rem Or false goto 2
:2
echo False
cls
exit

Here is the code:

@echo off
set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
     $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
           [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p
if "%password%"=="" goto 2
if "%password%"=="SuperUser" goto 1

:Again
set "psCommand=powershell -Command "$pword = read-host 'Wrong Password?. Try Again' -AsSecureString ; ^
     $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
           [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p
if "%password%"=="" goto 2
if "%password%"=="SuperUser" goto 1

:2
goto Again

:1
echo Valid password
pause>nul
Mofi
  • 46,139
  • 17
  • 80
  • 143