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

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

问题描述

我正在写一个批处理文件来执行一些其他程序。在这种情况下,我需要提示输入密码。我有任何方法来屏蔽输入文本。我不需要打印*******字符,而不是输入字符。 Linux的密码提示行为(打字时不打印)就足够了。

  @echo off 
SET / P variable =密码:
echo%variable%
暂停

这将读取输入,但我不能使用此方法掩盖文本。

$ b / b

解决方案

对于较早版本的Window,可以使用另一个包含的工具(VBScript) - 以下两个脚本完成你想要的工作。 p>

首先, getpwd.cmd

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

然后, getpwd.vbs



 设置oScriptPW = CreateObject(ScriptPW.Password)
strPassword = oScriptPW.GetPassword()
Wscript。 StdOut.WriteLine strPassword

说明如下:



getpwd.vbs 只是使用password对象从用户输入密码,然后将其打印到标准输出(下一段将解释为什么不显示在终端中)。



getpwd.cmd 有点棘手(但命令脚本通常是) p>

< nul:set / p passwd = Password:有点鬼祟 - 命令的效果输出没有尾随换行符的提示 - 这是从 bash echo -n c> shell。它将 passwd 设置为空字符串作为不相关的副作用,不会等待输入,因为它从 nul:

/ fdelims =%% i in('cscript / nologo getpwd.vbs') do set passwd = %% i语句是最棘手的位。它运行VBScript没有微软的广告,所以唯一的行输出是密码(从VBscript Wscript.StdOut.WriteLine strPassword



将分隔符设置为无需要用空格捕获输入行,否则你只得到第一个字。for ... do set ... 设置 passwd 作为从VBScript输出的实际密码。



回显一个空行(以终止Password:行),并且密码将位于 passwd 环境变量






现在,不幸的是, scriptpw.dll Windows中复制 scriptpw.dll 文件\ System32 Windows \System32 下将XP或Windows 2003系统的\ System32 code>文件夹。一旦DLL被复制,您将需要通过运行:

注册它。

  regsvr32 scriptpw.dll 

要在Vista及更高版本上成功注册DLL,您需要管理员权限。






如果你不想过度追踪和注册旧的DLL文件,还有另一种方法。后来版本的Windows(没有那个DLL)应该有Powershell可用。



你真的应该考虑升级你的脚本以完全使用它,因为它一种比 cmd.exe 更强大的脚本语言。但是,如果您希望将大量代码保存为 cmd.exe 脚本(例如,如果您有一个很多代码,



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

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

Powershell脚本同样简单:

  $ password = Read-Host输入密码-AsSecureString 
$ password = [Runtime.InteropServices.Marshal ] :: SecureStringToBSTR($ password)
$ password = [Runtime.InteropServices.Marshal] :: PtrToStringAuto($ password)
echo $ password


请记住,要运行本地未签名的Powershell脚本,请执行以下命令:p $ p>

<在你的机器上,你可能需要修改执行策略从(draconian,虽然非常安全)默认,如下:

  set-executionpolicy remersigned从Powershell本身中分配


I am writing a batch file for 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 behaviour (Print nothing while typing) is enough.

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

This will read the input but I cant mask the text using this approach.

解决方案

For older versions of Window, you can make use of another included tool (VBScript) - the following two scripts do the job you want.

First, getpwd.cmd:

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

Then, getpwd.vbs:

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

Explanation follows:

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 is a bit trickier (but command scripts usually are).

The "<nul: set /p passwd=Password: " is somewhat sneaky - the effect of the 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.

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".

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

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.


Now, unfortunately, scriptpw.dll is available with XP and 2003 but not later versions. In order to rectify this, you can simply copy the scriptpw.dll file from the Windows\System32 folder of an XP or Windows 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

To successfully register the DLL on Vista and later, you will need administrator privileges.


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

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.

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

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.

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

from within Powershell itself.

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

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