What would be the Windows batch equivalent for HTM

2019-01-09 10:48发布

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

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

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

11条回答
forever°为你锁心
2楼-- · 2019-01-09 10:54

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

read -s -p "Password: " PASSWORD

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!).

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

cmd /cyourbatchfile.bat $PASSWORD

All a bit convoluted and not at all satisfying ;)

查看更多
不美不萌又怎样
3楼-- · 2019-01-09 10:55

check out this

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
查看更多
淡お忘
4楼-- · 2019-01-09 10:58

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.

查看更多
看我几分像从前
5楼-- · 2019-01-09 10:59

1.Pure batch solution that (ab)uses XCOPY command and its /P /L switches found here :

:: 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/mshta file and should be saved as a .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:

@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);
查看更多
狗以群分
6楼-- · 2019-01-09 11:09

ConSet is a free tool written by Frank P. Westlake. It is an extended version of standard Windows command set.

ConSet.exe - Displays, sets, or deletes cmd.exe environment variables, modifies console parameters, and performs floating point mathematics.

As it is not a standard Windows console application, the usage of this tool requires either the distribution of this tool together with the batch file or the tool is stored on a server share and the batch file calls this tool directly from the server share.

ConSet makes a prompt for a password string with hidden input assigned to an environment variable very easy:

ConSet.exe /PH "PWD=Password: "

The additional parameter H results in hiding user input.

查看更多
走好不送
7楼-- · 2019-01-09 11:14

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.

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

Regards, divo

查看更多
登录 后发表回答