我可以在bat文件中屏蔽输入文本吗? [英] Can I mask an input text in a bat file?

查看:104
本文介绍了我可以在bat文件中屏蔽输入文本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个批处理文件以执行其他一些程序.在这种情况下,我需要提示输入密码.我有什么方法可以掩盖输入文本吗?我不需要打印*******字符而不是输入字符. Linux的密码提示行为(键入时不打印任何内容)就足够了.

I am writing a batch file to execute some other programs. In this case I need to prompt for a password. Do I have any way to mask the input text? I don't need to print ******* characters instead of input characters. Linux's Password prompt behavior (Print nothing while typing) is enough.

@echo off
SET /P variable=Password : 
echo %variable%
Pause

这将读取输入内容,但我无法使用这种方法屏蔽文本.

This will read the input but I can't mask the text using this approach.

推荐答案

在XP和Server 2003之前,您可以使用另一个附带的工具(VBScript)-以下两个脚本可以完成您想要的工作.

Up to XP and Server 2003, you can make use of another included tool (VBScript) - the following two scripts do the job you want.

首先,getpwd.cmd:

@echo off
<nul: set /p passwd=Password: 
for /f "delims=" %%i in ('cscript /nologo getpwd.vbs') do set passwd=%%i
echo.

然后,getpwd.vbs:

Set oScriptPW = CreateObject("ScriptPW.Password")
strPassword = oScriptPW.GetPassword()
Wscript.StdOut.WriteLine strPassword

getpwd.vbs仅使用密码对象从用户输入密码,然后将其打印到标准输出(下一段将说明为什么终端中未显示该密码).

The getpwd.vbs simply uses the password object to input the password from the user and then print it to standard output (the next paragraph will explain why that doesn't show up in the terminal).

getpwd.cmd命令脚本有点棘手,但它的基本工作原理如下.

The getpwd.cmd command script is a bit trickier but it basically works as follows.

"<nul: set /p passwd=Password: "命令的作用是输出没有尾随换行符的提示-这是一种从bash外壳模拟"echo -n"命令的偷偷摸摸的方法.它将passwd设置为空字符串,这是无关紧要的副作用,并且因为它从nul:设备获取输入,所以不等待输入.

The effect of the "<nul: set /p passwd=Password: " command is to output the prompt with no trailing newline character - it's a sneaky way to emulate the "echo -n" command from the bash shell. It sets passwd to an empty string as an irrelevant side effect and doesn't wait for input since it's taking its input from the nul: device.

"for /f "delims=" %%i in ('cscript /nologo getpwd.vbs') do set passwd=%%i"语句是最棘手的位.它在没有Microsoft广告"的情况下运行VBScript,因此唯一的输出是密码(来自VBscript "Wscript.StdOut.WriteLine strPassword".

The "for /f "delims=" %%i in ('cscript /nologo getpwd.vbs') do set passwd=%%i" statement is the trickiest bit. It runs the VBScript with no Microsoft "advertising", so that the only line output is the password (from the VBscript "Wscript.StdOut.WriteLine strPassword".

将定界符设置为空就不需要捕获整个带有空格的输入行,否则您只会得到第一个单词. "for ... do set ..."位将passwd设置为VBScript输出的实际密码.

Setting the delimiters to nothing is required to capture an entire input line with spaces, otherwise you just get the first word. The "for ... do set ..." bit sets passwd to be the actual password output from the VBScript.

然后,我们回显空白行(以终止"Password: "行),并且在代码运行后,密码将位于passwd环境变量中.

Then we echo a blank line (to terminate the "Password: " line) and the password will be in the passwd environment variable after the code has run.

现在,如前所述,scriptpw.dll仅在XP/2003之前可用.为了解决这个问题,您可以简单地将scriptpw.dll文件从XP/2003系统的Windows\System32文件夹复制到您自己的系统上的Winnt\System32Windows\System32文件夹.复制DLL后,您需要通过运行以下命令进行注册:

Now, as mentioned, scriptpw.dll is available only up to XP/2003. In order to rectify this, you can simply copy the scriptpw.dll file from the Windows\System32 folder of an XP/2003 system to the Winnt\System32 or Windows\System32 folder on your own system. Once the DLL has been copied, you will need to register it by running:

regsvr32 scriptpw.dll

要在Vista和更高版本上成功注册DLL,您将需要管理员权限.我还没有研究过这样一个举动的合法性.

To successfully register the DLL on Vista and later, you will need administrator privileges. I haven't examined the legality of such a move so cave lector.

如果您不是太热衷于尝试查找和注册较早的DLL文件(出于方便或法律原因),则可以采用另一种方法. Windows的更高版本(没有必需的DLL)应该具有Powershell.

If you're not overly keen on trying to track down and register older DLL files (for convenience or legal reasons), there is another way. Later versions of Windows (the ones that don't have the required DLL) should have Powershell available to you.

事实上,您确实应该考虑升级脚本以使其完全使用,因为它是比cmd.exe更强大的脚本语言.但是,如果要将大部分代码保留为cmd.exe脚本(例如,如果有不想转换的 lot 代码),则可以使用相同的技巧

And, in fact, you really should consider upgrading your scripts to use it fully since it's a much more capable scripting language than cmd.exe. However, if you want to keep the bulk of your code as cmd.exe scripts (such as if you have a lot of code that you don't want to convert), you can use the same trick.

首先,修改cmd脚本,使其调用Powershell而不是CScript:

First, modify the cmd script so it calls Powershell rather than CScript:

@echo off
for /f "delims=" %%i in ('powershell -file getpwd.ps1') do set passwd=%%i

Powershell脚本同样简单:

The Powershell script is equally simple:

$password = Read-Host "Enter password" -AsSecureString
$password = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$password = [Runtime.InteropServices.Marshal]::PtrToStringAuto($password)
echo $password

尽管进行了一些编组以获取实际的密码文本.

although with some marshalling to get the actual password text.

请记住,要在您的计算机上运行本地未签名的Powershell脚本,您可能需要使用以下内容修改(严格的,虽然很安全的)默认执行策略:

Remember that, to run local unsigned Powershell scripts on your machine, you may need to modify the execution policy from the (draconian, though very safe) default, with something like:

set-executionpolicy remotesigned

在Powershell本身中.

from within Powershell itself.

这篇关于我可以在bat文件中屏蔽输入文本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆