我可以掩盖一个bat文件输入文本 [英] Can I mask an input text in a bat file

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

问题描述

我写执行一些其他程序的批处理文件。在这种情况下,我需要提示输入密码。难道我有什么办法来掩盖输入文本。我并不需要打印的 *** 字符,而不是输入的字符。 Linux的的密码提示行为(打印什么,而打字)就足够了。

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.

推荐答案

对于旧版本的窗口,你可以利用其它附带的工具(VBScript)的的 - 下面两个脚本做你想要的工作。

For older versions of Window, 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 是有点棘手(但命令脚本通常是)。

getpwd.cmd is a bit trickier (but command scripts usually are).

&LT; NUL:设置/ p passwd文件=密码:有点偷偷摸摸的 - 这个命令的作用是输出没有尾随换行符的提示 - 这是效仿回声-n从庆典命令偷偷摸摸。它集 passwd文件来一个空字符串作为一个不相干的副作用,不等待输入,因为它是从服用其输入NUL:设备。

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.

FOR / Fdelims =%% i的('CSCRIPT / NOLOGO getpwd.vbs')做设置的passwd = %%我语句是最棘手位。它运行没有微软的广告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".

设置分隔符没有什么需要捕捉空格输入行,否则你只是得到第一个字。在为......别设置... passwd文件来从VBScript的输出实际的密码。

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.

然后,我们回显一个空行(终止密码:线)和密码会在 passwd文件环境变量code运行之后。

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而不是更高版本。为了纠正这一点,你可以简单地复制从XP的 Windows \\ System32下文件夹中的 scriptpw.dll 文件或Windows 2003系统的 WINNT \\ SYSTEM32 Windows \\ System32下在自己的系统上的文件夹中。一旦DLL已被复制,您将需要通过运行来注册吧:

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

要成功注册在Vista DLL和之后,你将需要管理员权限。

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

如果你不是在试图追查并注册旧的DLL文件过于热衷,还有另一种方式。更高版本的Windows(即不具有该DLL的那些)应该有PowerShell的提供给你。

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.

您真的应该考虑升级你的脚本,因为它是一个更能干的脚本语言比的cmd.exe 来充分利用它。但是,如果你想保持你的大部分code作为的cmd.exe 脚本(比如,如果你有的的的手$您不想要转换C $ C),可以使用同样的伎俩。

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