HTML 的 input type="password" 的 Windows 批处理等效项是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/286871/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:36:08  来源:igfitidea点击:

What would be the Windows batch equivalent for HTML's input type="password"?

windowsbatch-filepasswordscmd

提问by Nano Taboada

I need to get authentication credentials from the users within a Windows script but the classic "first Google result" approach:

我需要从 Windows 脚本中的用户那里获取身份验证凭据,但使用经典的“第一个 Google 结果”方法:

SET /P USR=Username: 
SET /P PWD=Password: 

is less than satisfying, so I was wondering if there's let's say an "equivalent" to HTML's input type="password"?

不太令人满意,所以我想知道是否可以说“等效于” HTML 的输入 type="password"

Any comment would be really appreciated, thanks much in advance!

任何评论将不胜感激,非常感谢!

采纳答案by Nano Taboada

check out this

看看这个

http://www.netikka.net/tsneti/info/tscmd052.htm

http://www.netikka.net/tsneti/info/tscmd052.htm

@echo off & setlocal enableextensions
    :: Build a Visual Basic Script
    set vbs_=%temp%\tmp$$$.vbs
    set skip=
    findstr "'%skip%VBS" "%~f0" > "%vbs_%"
    ::
    :: Prompting without linefeed as in Item #15
    echo.|set /p="Password: "

    :: Run the script with Microsoft Windows Script Host Version 5.6
    for /f "tokens=* delims=" %%a in ('cscript //nologo "%vbs_%"') do set MyPass1=%%a

    ::
    ::echo.
    echo.|set /p="Retype  : "

    for /f "tokens=* delims=" %%a in ('cscript //nologo "%vbs_%"') do set MyPass2=%%a
    ::

    :: Clean up
    for %%f in ("%vbs_%") do if exist %%f del %%f
    ::
    :: Demonstrate the result
    echo.
    if "%MyPass1%"=="%MyPass2%" (
      echo The entered password was %MyPass1%
      ) else (
      echo No match)
    endlocal & goto :EOF
    '
    'The Visual Basic Script
    Set WshPass = WScript.CreateObject("ScriptPW.Password") 'VBS
    Password=WshPass.GetPassWord() 'VBS
    WScript.Echo PassWord 'VBS

回答by paxdiablo

By judicious use of another tool freely available on Windows, the following two scripts do the job you want.

通过明智地使用 Windows 上免费提供的另一个工具,以下两个脚本可以完成您想要的工作。

First, GetPwd.cmd:

首先,GetPwd.cmd:

@echo off
:: GetPwd.cmd - Get password with no echo.
<nul: set /p passwd=Password: 
for /f "delims=" %%i in ('cscript /nologo GetPwd.vbs') do set passwd=%%i
echo.
:: This bit's just to prove we have the password.
echo %passwd%

Then, GetPwd.vbs:

然后,GetPwd.vbs:

' GetPwd.vbs - Get password with no echo then echo it. '
Set oScriptPW = CreateObject("ScriptPW.Password")
strPassword = oScriptPW.GetPassword()
Wscript.StdOut.WriteLine strPassword

Explanation:

解释:

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

GetPwd.vbs 简单地使用密码对象从用户输入密码,然后将其打印到标准输出(下一段将解释为什么没有显示在终端中)。

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

GetPwd.cmd 有点棘手(但命令脚本通常是)。

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

"<nul: set /p passwd=Password: "命令只是输出没有尾随 CR/LF 的提示 - 这是一种模拟 bash 的"echo -n". 它设置passwd为空字符串作为副作用并且不等待输入,因为它从 nul: 设备获取输入。

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 (/nologo), so that the only line output is the password (from the vbscript "Wscript.StdOut.WriteLine strPassword".

"for /f "delims=" %%i in ('cscript /nologo GetPwd.vbs') do set passwd=%%i"声明是最棘手位。它运行没有 Microsoft 广告 ( /nologo)的 vbscript ,因此唯一的行输出是密码(来自 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 passwdto be the actual password output from the vbscript.

捕获带空格的输入行不需要将分隔符设置为空,否则您只会得到第一个单词。这些"for ... do set ..."集合passwd是 vbscript 的实际密码输出。

Then we echo a blank line (actually terminate the "Password: "line) and echo the password so you can verify it works:

然后我们回显一个空行(实际上是终止该"Password: "行)并回显密码,以便您验证它是否有效:

C:\Pax> GetPwd
Password:
this is my password

C:\Pax> 

The scriptpw.dll is available with XP and 2K3 but not necessarily later versions.

scriptpw.dll 适用于 XP 和 2K3,但不​​一定适用于更高版本。

Instructions for Vista and presumably Win7 are below, give them a try:

Vista 和大概是 Win7 的说明如下,试试看:

To mask the password, the script takes advantage of the ScriptPW COM object. ScriptPW is loaded by default on Windows XP and Windows 2003. If you're running Windows 2000 or Windows Vista, you will need to copy the scriptpw.dllfile from the Windows\System32folder of an XP system, or Windows 2003 system to the Winnt\System32or Windows\System32folder on your Windows 2000 or Vista system. Once the DLL has been copied, you will need to register it by running the command:

regsvr32 scriptpw.dll

To successfully register the DLL on a Vista machine, you will need to open the command prompt as administrator. To do this, click Start | All Programs | Accessories. Then right-click on the Command Prompt shortcut and select “Run as administrator.” Once at the command prompt as administrator, you'll be able to successfully run the regsvr32 scriptpw.dllcommand to register the DLL.

为了屏蔽密码,脚本利用了 ScriptPW COM 对象。ScriptPW 在 Windows XP 和 Windows 2003 上默认加载。如果您运行的是 Windows 2000 或 Windows Vista,则需要将scriptpw.dll文件从Windows\System32XP 系统或 Windows 2003 系统的文件夹复制到 Windows 2000 上的Winnt\System32Windows\System32文件夹或 Vista 系统。复制 DLL 后,您需要通过运行以下命令来注册它:

regsvr32 scriptpw.dll

要在 Vista 计算机上成功注册 DLL,您需要以管理员身份打开命令提示符。为此,请单击开始 | 所有程序 | 配件。然后右键单击命令提示符快捷方式并选择“以管理员身份运行”。以管理员身份进入命令提示符后,您将能够成功运行该regsvr32 scriptpw.dll命令来注册 DLL。

回答by npocmaka

1.Pure batch solutionthat (ab)uses XCOPYcommand and its /P /Lswitches found here:

1.纯批处理解决方案,(ab)使用这里找到的XCOPY命令及其/P /L开关:

:: Hidden.cmd
::Tom Lavedas, 02/05/2013, 02/20/2013
::Carlos, 02/22/2013
::https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/f7mb_f99lYI


@Echo Off
:HInput
SetLocal EnableExtensions EnableDelayedExpansion
Set "FILE=%Temp%.\T"
Set "FILE=.\T"
Keys List >"%File%"
Set /P "=Hidden text ending with Ctrl-C?: " <Nul
Echo.
Set "HInput="
:HInput_
For /F "tokens=1* delims=?" %%A In (
 '"Xcopy /P /L "%FILE%" "%FILE%" 2>Nul"'
) Do (
  Set "Text=%%B"
  If Defined Text (
    Set "Char=!Text:~1,1!"
    Set "Intro=1"
    For /F delims^=^ eol^= %%Z in ("!Char!") Do Set "Intro=0"
    Rem If press Intro
    If 1 Equ !Intro! Goto :HInput#
    Set "HInput=!HInput!!Char!"
  )
)
Goto :HInput_
:HInput#
Echo(!HInput!
Goto :Eof 

2.Password submitter that uses a HTA pop-up. This is a hybrit .bat/jscript/mshtafile and should be saved as a .bat:

2.使用 HTA 弹出窗口的密码提交器这是一个 hybrit .bat/jscript/mshta文件,应保存为 .bat:

<!-- :
:: PasswordSubmitter.bat
@echo off
for /f "tokens=* delims=" %%p in ('mshta.exe "%~f0"') do (
    set "pass=%%p"
)

echo your password is %pass%
exit /b
-->

<html>
<head><title>Password submitter</title></head>
<body>

    <script language='javascript' >
        function pipePass() {
            var pass=document.getElementById('pass').value;
            var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
            close(fso.Write(pass));

        }
    </script>

    <input type='password' name='pass' size='15'></input>
    <hr>
    <button onclick='pipePass()'>Submit</button>

</body>
</html>

3.A self-compiled .net hybrid.Again should be saved as .bat.In difference with other solutions it will create/compile a small .exe file that will be called (if you wish you can delete it). Also requires installed .net framework but that's rather not a problem:

3.一个自编译的.net混合.Again应该另存为.bat.与其他解决方案不同,它将创建/编译一个将被调用的小.exe文件(如果您愿意可以删除它)。还需要安装 .net 框架,但这不是问题:

@if (@X)==(@Y) @end /* JScript comment
@echo off
setlocal

for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d  /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do (
   set "jsc=%%v"
)

if not exist "%~n0.exe" (
    "%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0"
)

for /f "tokens=* delims=" %%p in ('"%~n0.exe"') do (
    set "pass=%%p"
)

echo your password is %pass%

endlocal & exit /b %errorlevel%

*/



import System;



var pwd = "";
var key;

Console.Error.Write("Enter password: ");

        do {
           key = Console.ReadKey(true);
           if ( (key.KeyChar.ToString().charCodeAt(0)) >= 20 && (key.KeyChar.ToString().charCodeAt(0) <= 126) ) {
              pwd=pwd+(key.KeyChar.ToString());
              Console.Error.Write("*");
           }   

        } while (key.Key != ConsoleKey.Enter);
        Console.Error.WriteLine();
        Console.WriteLine(pwd);

回答by Dirk Vollmar

I assume that you want no echo of the password on the screen.

我假设您不想在屏幕上回显密码。

If a pop-up window is ok for you, you could use e.g. VBScript to show an IE window displaying a password field. Here's an example.

如果弹出窗口适合您,您可以使用例如 VBScript 来显示显示密码字段的 IE 窗口。这是一个例子

As an alternative you could call your script from an HTA (HTML Application) file (see Introduction to HTML Applications (HTAs).

作为替代方案,您可以从 HTA(HTML 应用程序)文件中调用您的脚本(请参阅HTML 应用程序简介 (HTA)

Regards, divo

问候,迪沃

回答by Michael Hinds

If you can install Cygwin, you'll get a bash shell by default, so this command will work:

如果您可以安装 Cygwin,则默认情况下您将获得一个 bash shell,因此此命令将起作用:

read -s -p "Password: " PASSWORD

read -s -p "密码:" 密码

Only problem is now the value of PASSWORD is only set in the bash shell, not as an environment variable a batch file can see (don't use PWD as this means something else in cygwin). So you would have to rewrite your script as a bash shell script (maybe not too hard given the limitations of the command prompt!).

现在唯一的问题是 PASSWORD 的值仅在 bash shell 中设置,而不是作为批处理文件可以看到的环境变量(不要使用 PWD,因为这意味着 cygwin 中的其他内容)。因此,您必须将脚本重写为 bash shell 脚本(鉴于命令提示符的限制,可能不会太难!)。

Or you could pass the password into a batch script from cygwin, but this means running a new instance of the command prompt:

或者您可以将密码从 cygwin 传递到批处理脚本中,但这意味着运行命令提示符的新实例:

cmd /cyourbatchfile.bat $PASSWORD

cmd /cyourbatchfile.bat $PASSWORD

All a bit convoluted and not at all satisfying ;)

有点令人费解,一点也不令人满意;)

回答by Keng

We do stuff like this all the time but put the password in the commandline and pass it to a variable in the batch file.

我们一直在做这样的事情,但将密码放在命令行中并将其传递给批处理文件中的变量。

回答by Bill_Stewart

another alternative is my EditV32 (x86) or EditV64 (x64) command-line tools. For example:

另一种选择是我的 EditV32 (x86) 或 EditV64 (x64) 命令行工具。例如:

editv32 -m -p "Password: " PWD

-mmeans "masked input" and -pis the prompt. The user's input is stored in the PWD environment variable. You can get it here:

-m表示“屏蔽输入”,-p是提示。用户的输入存储在 PWD 环境变量中。你可以在这里得到它:

http://www.westmesatech.com/editv.html

http://www.westmesatech.com/editv.html

回答by Panos Rontogiannis

Another approach is to call PowerShellcommands from your Batchscript. Here's an example that configures the logon account of a service:

另一种方法是PowerShellBatch脚本中调用命令。以下是配置服务登录帐户的示例:

$password = Read-Host "Enter password" -AsSecureString;
$decodedpassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password));
& "sc.exe" config THE_SERVICE_NAME obj= THE_ACCOUNT password= $decodedPassword;

where THE_SERVICE_NAME is the name of the service to configure and THE_ACCOUNT is the logon account.

其中 THE_SERVICE_NAME 是要配置的服务的名称,THE_ACCOUNT 是登录帐户。

Then we can use it from a batch script like that:

然后我们可以从这样的批处理脚本中使用它:

call powershell -Command "$password = Read-Host "Enter password" -AsSecureString; $decodedpassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)); & "sc.exe" config THE_SERVICE_NAME obj= THE_ACCOUNT password= $decodedPassword;"

which is simply calling PowerShell.exe and passing the three commands.

这只是调用 PowerShell.exe 并传递三个命令。

The advantage of this approach is that the majority of Windows installations today include PowerShell, so no extra program or script is needed. The drawback is that you will need to either use the password inside the PowerShellcall (like in my example) or store it in an environment variable and then use it from your batch script. I preffer the former because it is more secure and simpler.

这种方法的优点是当今大多数 Windows 安装都包含PowerShell,因此不需要额外的程序或脚本。缺点是您需要在PowerShell调用中使用密码(如在我的示例中)或将其存储在环境变量中,然后从批处理脚本中使用它。我更喜欢前者,因为它更安全、更简单。

回答by Bill_Stewart

Another alternative is ReadLine.exe. Example:

另一种选择是ReadLine.exe。例子:

@echo off
setlocal enableextensions
set PASSWORD=
for /f "delims=" %%p in ('readline -h -p "Enter password: "') do set PASSWORD=%%p
echo You entered: %PASSWORD%
endlocal

回答by Aacini

You may use ReadFormattedLine subroutine for all kind of formatted input. For example, the commands below read an username and password of 8 characters each, display asterisks in the screen, and continue automatically with no need to press Enter:

您可以将 ReadFormattedLine 子程序用于所有类型的格式化输入。例如,下面的命令读取每个 8 个字符的用户名和密码,在屏幕上显示星号,并自动继续,无需按 Enter:

call :ReadFormattedLine USR="********" /M "Username: "
call :ReadFormattedLine PWD="********" /M "Password: "

Or in a different way:

或者以不同的方式:

call :ReadFormattedLine nameAndPass="******** / ********" /M "Enter Username / Password: "

In previous example, when the user completed the username, the subroutine display the slash and read the password; if the user delete characters, the slash is also deleted automatically.

在前面的例子中,当用户完成用户名时,子程序显示斜杠并读取密码;如果用户删除字符,斜线也会自动删除。

This subroutine is written in pure Batch so it does not require any additional program, and it allows several formatted input operations, like read just numbers, convert letters to uppercase, etc. You may download ReadFormattedLine subroutine from Read a line with specific format.

这个子程序是纯Batch编写的,不需要任何额外的程序,它允许几种格式化的输入操作,比如只读数字,将字母转换为大写等。您可以从读取特定格式的行下载ReadFormattedLine子程序。