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

查看:36
本文介绍了我可以屏蔽 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 shell 的 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 系统的 WindowsSystem32 文件夹复制到 WinntSystem32WindowsSystem32 文件夹在您自己的系统上.复制 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 WindowsSystem32 folder of an XP/2003 system to the WinntSystem32 or WindowsSystem32 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 脚本(例如,如果您有很多不想转换的代码),您可以使用相同的技巧.

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天全站免登陆