如何使用 **** 在命令行中隐藏密码,并将值获取到 .bat 文件和 .sh 文件中

18

我用的.bat文件命令可以从命令行获取用户输入:

My .bat文件命令可以从命令行获取用户输入:

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

我在bash脚本中获取用户输入的.sh文件命令是

echo -n "Enter weblogic password: "
read weblogicpassword

现在,当我们输入密码时,这些值是可见的,显示在命令行中。 我们如何从命令行获取密码值,而这些值对用户来说应该是不可见的,就像**。


@Boann,这个问题漏掉了关于bash的部分。 - RedX
1
我已经检查过了,但是从命令行中没有掩码化类型 ****。 - Obulesu Bukkana
你能帮我解决一个关于如何在批处理文件中以类似****或bash的静默模式输入密码的问题吗? - Obulesu Bukkana
4个回答

15

通过使用 PowerShell,我们可以在32位和64位系统上实现此目标。

 @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%

通过使用这个方法,我们可以在命令行中获取带有***的密码。

在Bash脚本中,我们可以通过使用以下代码实现此目的。

#!/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" 

read命令的选项包括: -p:提示字符串。 -r:不使用反斜线作为转义字符。 -s:静默模式,输入不会被回显。 -n 1:输入的字符数量。

除非遇到\0,否则read返回0,并且用户键入的字符被放置在char变量中。

IFS=部分清除IFS变量,这确保您键入的任何空格或制表符字符都包含在密码中,而不是被read解析出来。


在删除字符之前,您需要添加一个额外的检查,以确保密码不为空,否则将删除实际提示。 - Nathan
你如何通过调用批处理版本的PowerShell来运行此脚本,然后检查是否有输入? - rockower

14

在bash中,使用read -s命令。

-s是静默模式。如果输入来自终端,则不会回显字符。

对于批处理文件,似乎更为复杂。

在此处阅读更多信息: Can I mask an input text in a bat file


这个方法的实现可能看起来像这样:a=''; while read -n 1 -s c; do [ "$c" = '' ] && break; echo -n '*'; a="$a$c"; done。但是这不允许使用退格键。 - Alfe
2
带有退格键功能: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
现在我在使用你的命令在Bash脚本中获取***代替字符,请问你能帮我在Bat文件中实现同样的功能吗? - Obulesu Bukkana

8
这里提供了一个适用于32位Windows系统的解决方案。
@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

那是什么奇怪的字符串? hP1X500P [PZBBBfh#b ##fXf-V @ $fPf] f3 / f1 / 5 ++ u5`? - RedX
1
这是由Herbert Kleebauer创建的Ascii二进制文件。你可以在Usenet帖子中找到他创建的许多Ascii二进制文件。 - foxidrive
请参考 http://social.technet.microsoft.com/Forums/scriptcenter/en-US/7752db4f-72ce-4538-8963-bf8f7676b45d/masking-password-through-cmd-file?forum=ITCG 获取 64 位替代方案。 - RedX

0

我阅读了所有的答案并且修复了其中一个。

这段代码会要求用户输入密码。你可以在 if "%password%"=="SuperUser" 中更改密码。

它会检查输入,如果输入是有效的(即 SuperUser),则会跳转到标签 1

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

这里是代码:

@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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接