我需要从Windows脚本中获取用户的身份验证凭据,但经典的“第一个Google结果”方式已不再适用:
SET /P USR=Username:
SET /P PWD=Password:
使用HTML中的来输入密码,但其效果并不是很令人满意。那么是否有类似的“等价物”可以替代它呢?
非常感谢您提供任何建议!
我需要从Windows脚本中获取用户的身份验证凭据,但经典的“第一个Google结果”方式已不再适用:
SET /P USR=Username:
SET /P PWD=Password:
使用HTML中的来输入密码,但其效果并不是很令人满意。那么是否有类似的“等价物”可以替代它呢?
非常感谢您提供任何建议!
检查这个
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
通过在Windows上免费使用另一个工具,以下两个脚本可以完成您想要的工作。
首先是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%
然后,GetPwd.vbs:
' GetPwd.vbs - Get password with no echo then echo it. '
Set oScriptPW = CreateObject("ScriptPW.Password")
strPassword = oScriptPW.GetPassword()
Wscript.StdOut.WriteLine strPassword
解释:
GetPwd.vbs 简单地使用密码对象从用户输入密码,然后将其打印到标准输出(下一段将解释为什么这不会在终端中显示)。
GetPwd.cmd 有点棘手(但命令脚本通常都是这样的)。
"<nul: set /p passwd=Password: "
命令只是输出提示符,没有尾随的 CR/LF - 这是一种模拟 bash 的 "echo -n"
的巧妙方法。它将 passwd
设置为空字符串作为副作用,并且不等待输入,因为它正在从 nul: 设备获取输入。
"for /f "delims=" %%i in ('cscript /nologo GetPwd.vbs') do set passwd=%%i"
语句是最棘手的部分。它以无 Microsoft 广告的方式运行 vbscript(/nologo
),因此唯一的输出行是密码(来自 vbscript 的 "Wscript.StdOut.WriteLine strPassword"
)。
将定界符设置为 nothing 是必需的,以捕获带有空格的输入行,否则您只会得到第一个单词。 "for ... do set ..."
将 passwd
设置为实际从 vbscript 输出的密码。
然后我们输出一个空行(实际上是终止 "Password: "
行),并输出密码以便您可以验证它是否有效:
C:\Pax> GetPwd
Password:
this is my password
C:\Pax>
脚本 scriptpw.dll
可在XP和2K3中使用,但不一定适用于更高版本。
下面是Vista和可能的Win7的说明,请试试:
为了掩盖密码,该脚本利用ScriptPW COM对象。ScriptPW默认加载在Windows XP和Windows 2003上。如果您正在运行Windows 2000或Windows Vista,则需要从XP系统或Windows 2003系统的
Windows\System32
文件夹中复制scriptpw.dll
文件到您的Windows 2000或Vista系统的Winnt\System32
或Windows\System32
文件夹。一旦DLL已被复制,您将需要通过运行以下命令来注册它:
regsvr32 scriptpw.dll
要在Vista机器上成功注册DLL,您需要以管理员身份打开命令提示符。要执行此操作,请单击“开始”|“所有程序”|“附件”。然后右键单击命令提示符快捷方式并选择“以管理员身份运行”。一旦以管理员身份进入命令提示符,您将能够成功运行
regsvr32 scriptpw.dll
命令来注册DLL。
:: 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
<!-- :
:: 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>
@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);
我猜你不想在屏幕上显示密码。
如果弹出窗口对你来说可以接受,你可以使用VBScript来显示一个IE窗口,其中包含一个密码字段。这里是一个示例。
作为替代方案,你可以从HTA(HTML应用程序)文件中调用你的脚本(请参见HTML应用程序(HTAs)介绍)。
祝好, divo
如果您可以安装Cygwin,系统默认会有一个bash shell,因此此命令将起作用:
read -s -p "Password: " PASSWORD
唯一的问题是现在PASSWORD的值只在bash shell中设置,而不是作为批处理文件可见的环境变量(不要使用PWD,因为在cygwin中表示其他意思)。因此,您需要将脚本重写为bash shell脚本(也许不太难,考虑到命令提示符的限制!)。
或者您可以从cygwin将密码传递给批处理脚本,但这意味着运行新的命令提示符实例:
cmd /cyourbatchfile.bat $PASSWORD
有点复杂,且不太令人满意 ;)
call :ReadFormattedLine USR="********" /M "Username: "
call :ReadFormattedLine PWD="********" /M "Password: "
call :ReadFormattedLine nameAndPass="******** / ********" /M "Enter Username / Password: "
$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;
其中THE_SERVICE_NAME是要配置的服务名称,THE_ACCOUNT是登录帐户。
然后我们可以像这样从批处理脚本中使用它:
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;"
这只是调用PowerShell.exe并传递三个命令。
这种方法的优点是,大多数Windows安装程序今天都包括PowerShell
,因此不需要额外的程序或脚本。缺点是您需要在PowerShell
调用中使用密码(就像我的示例中一样),或者将其存储在环境变量中,然后从批处理脚本中使用它。我更喜欢前者,因为它更安全和简单。
ConSet是由Frank P. Westlake编写的免费工具。它是标准Windows命令set的扩展版本。
ConSet.exe - 显示、设置或删除cmd.exe环境变量,修改控制台参数并执行浮点数学运算。
由于它不是标准的Windows控制台应用程序,因此使用此工具需要将该工具与批处理文件一起分发,或者将该工具存储在服务器共享中,并且批处理文件直接从服务器共享调用该工具。
ConSet使得为隐藏输入分配环境变量的密码字符串创建提示非常容易:
ConSet.exe /PH "PWD=Password: "
额外的参数H
会隐藏用户输入。
我编写了一个名为editenv
的开源工具,它取代了我以前的editv32
/editv64
实用程序:
https://github.com/Bill-Stewart/editenv
它提供了--maskinput
(-m
)选项 [*],可让您隐藏键入的输入。示例:editenv --maskinput --prompt="Password: " PWD
Password:
提示符,你输入的任何内容都会被放置在PWD
环境变量中。https://github.com/Bill-Stewart/editenv/releases
[*] 请注意,--maskinput
(-m
)选项不安全 -- 输入的内容以明文形式放置在环境中。该功能仅为方便而设。