Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to hide password in command line with **** and get the value into .bat file and .sh file [duplicate]

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 **

like image 998
Obulesu Bukkana Avatar asked Nov 13 '13 09:11

Obulesu Bukkana


People also ask

How do I add a password to command prompt?

Open Command Prompt, type net user Username * > press Enter key > Type New Password and Retype the New Password to confirm.

Can batch file input password automatically to CMD?

If you use the "net use" command and provide the username, the batch file will automatically ask you to enter the password; it doesn't echo any input. The response will be: Enter your username: username Type the password for \\UNC_PATH: I wrote a batch file that included this type of "masking" a few years ago.


2 Answers

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.

like image 126
Obulesu Bukkana Avatar answered Oct 11 '22 01:10

Obulesu Bukkana


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

like image 36
RedX Avatar answered Oct 11 '22 01:10

RedX