Wednesday, 15 May 2013

cmd - Can I mask an input text in a bat file -



cmd - Can I mask an input text in a bat file -

i writing batch file execute other programs. in case need prompt password. have way mask input text. don't need print *** characters instead of input characters. linux's password prompt behaviour (print nil while typing) enough.

@echo off set /p variable=password : echo %variable% pause

this read input cant mask text using approach.

for older versions of window, can create utilize of included tool (vbscript) - next 2 scripts job want.

first, getpwd.cmd:

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

then, getpwd.vbs:

set oscriptpw = createobject("scriptpw.password") strpassword = oscriptpw.getpassword() wscript.stdout.writeline strpassword

explanation follows:

the getpwd.vbs uses password object input password user , print standard output (the next paragraph explain why doesn't show in terminal).

getpwd.cmd bit trickier (but command scripts are).

the "<nul: set /p passwd=password: " sneaky - effect of command output prompt no trailing newline character - it's sneaky way emulate "echo -n" command bash shell. sets passwd empty string irrelevant side effect , doesn't wait input since it's taking input nul: device.

the "for /f "delims=" %%i in ('cscript /nologo getpwd.vbs') set passwd=%%i" statement trickiest bit. runs vbscript no microsoft "advertising", line output password (from vbscript "wscript.stdout.writeline strpassword".

setting delimiters nil required capture input lines spaces, otherwise first word. "for ... set ..." sets passwd actual password output vbscript.

then echo blank line (to terminate "password: " line) , password in passwd environment variable after code has run.

now, unfortunately, scriptpw.dll available xp , 2003 not later versions. in order rectify this, can re-create scriptpw.dll file windows\system32 folder of xp or windows 2003 scheme winnt\system32 or windows\system32 folder on own system. 1 time dll has been copied, need register running:

regsvr32 scriptpw.dll

to register dll on vista , later, need administrator privileges.

if you're not overly keen on trying track downwards , register older dll files, there way. later versions of windows (the ones don't have dll) should have powershell available you.

you should consider upgrading scripts utilize since it's much more capable scripting language cmd.exe. however, if want maintain mass of code cmd.exe scripts (such if have lot of code don't want convert), can utilize same trick.

first, modify cmd script calls powershell rather cscript:

@echo off /f "delims=" %%i in ('powershell -file getpwd.ps1') set passwd=%%i

the powershell script as simple:

$password = read-host "enter password" -assecurestring $password = [runtime.interopservices.marshal]::securestringtobstr($password) $password = [runtime.interopservices.marshal]::ptrtostringauto($password) echo $password

although marshalling actual password text.

remember that, run local unsigned powershell scripts on machine, may need modify execution policy (draconian, though safe) default, like:

set-executionpolicy remotesigned

from within powershell itself.

batch-file cmd command-prompt

No comments:

Post a Comment