【问题标题】:how to hide password in command line with **** and get the value into .bat file and .sh file [duplicate]如何使用 **** 在命令行中隐藏密码并将值放入 .bat 文件和 .sh 文件 [重复]
【发布时间】:2013-11-25 20:49:30
【问题描述】:

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

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

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

echo -n "Enter weblogic password: "
read weblogicpassword

现在当我们输入一些密码值时,这些值在命令行中是可见的。 我们如何从命令行获取对 **

等用户不可见的密码值

【问题讨论】:

  • @Boann 这个问题错过了关于 bash 的部分。
  • 我已经检查过了,但是命令行中没有 **** 类型的掩码
  • 在从批处理文件中输入时,您能否帮助我了解如何让密码像 **** 或 bash 类型的静默模式一样不可见

标签: bash batch-file cmd command-prompt


【解决方案1】:

通过使用 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" 

读取命令的选项有: -p :提示字符串。 -r :不要使用反斜杠作为转义字符。 -s :静音模式,输入不回显。 -n 1 : 要输入的字符数。

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

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

【讨论】:

  • 在删除字符之前,您需要额外检查密码是否为空,否则您将删除实际提示。
  • 如何通过在批处理版本中调用powershell来运行这个脚本,然后检查是否输入了一些东西?
【解决方案2】:

对于 bash,它的 read -s

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

对于批处理,它似乎更复杂。

在此处阅读: 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。但是,这不允许使用退格键。
  • 带退格功能: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;
  • 现在我正在使用你的命令在 bash 脚本中用 *** 代替字符。你也可以在 bat 文件中帮助我吗?
【解决方案3】:

这是适用于 Windows 32 位的解决方案。

@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`?
  • 这是 Herbert Kleebauer 的 Ascii 二进制文件。您会在 Usenet 帖子中找到他创建的许多帖子。
【解决方案4】:

我阅读了所有答案并修复了一个。

此代码要求用户输入密码。您可以在if "%password%"=="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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-21
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    相关资源
    最近更新 更多