bash 如何使用 **** 在命令行中隐藏密码并将值放入 .bat 文件和 .sh 文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19950620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to hide password in command line with **** and get the value into .bat file and .sh file
提问by Obulesu Bukkana
My .bat file command to get user input from command line is
我从命令行获取用户输入的 .bat 文件命令是
set /p weblogicpassword=Enter weblogic password:%=%
My .sh file command to get user input from bash script is
我从 bash 脚本获取用户输入的 .sh 文件命令是
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 **
现在,当我们为密码输入一些值时,这些值在命令行中可见。我们如何从命令行获取密码值,这些值应该对像**这样的用户不可见
采纳答案by RedX
For bash its read -s
.
对于 bash 其read -s
.
-s Silent mode. If input is coming from a terminal, characters are not echoed.
-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
在这里阅读: 我可以屏蔽 bat 文件中的输入文本吗
回答by Obulesu Bukkana
By using powershell, we can achieve this in 32 as well as 64 bit.
通过使用 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%
By using this we can get password with *** in command line
通过使用它,我们可以在命令行中使用 *** 获取密码
In bash script we can achieve this by using below code.
在 bash 脚本中,我们可以使用以下代码来实现这一点。
#!/bin/bash
prompt="Enter Password:"
while IFS= read -p "$prompt" -r -s -n 1 char
do
if [[ $char == $'@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
' ]]; then
break
fi
if [[ $char == $'7' ]]; 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.
读取命令的选项是: -p :提示字符串。-r :不要使用反斜杠作为转义字符。-s :静默模式,不回显输入。-n 1 : 要输入的字符数。
read returns 0 unless \0 is encountered, and the character the user types is placed into the char variable.
除非遇到 \0,否则 read 返回 0,并且用户键入的字符被放入 char 变量中。
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.
IFS= 部分清除 IFS 变量,这确保您键入的任何空格或制表符都包含在密码中,而不是通过读取解析出来。
回答by foxidrive
Here is a solution for Windows 32 bit.
这是 Windows 32 位的解决方案。
:1
echo True
cls
exit
rem Or false goto 2
:2
echo False
cls
exit
回答by i cant csharp
I read all answers and I fixed one.
我阅读了所有答案并修复了一个。
This code asks user for password. You can change password in if "%password%"=="SuperUser"
.
此代码要求用户输入密码。您可以在 中更改密码if "%password%"=="SuperUser"
。
It checks input and if input is valid (SuperUser), it goes to label 1
.
它检查输入,如果输入有效(超级用户),它会转到 label 1
。
@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
Here is the code:
这是代码:
##代码##