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.
Up to XP and Server 2003, you can make use of another included tool (VBScript) - the following two scripts do the job you want.
First,
getpwd.cmd
:Then,
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).The
getpwd.cmd
command script is a bit trickier but it basically works as follows.The effect of the
"<nul: set /p passwd=Password: "
command is to output the prompt with no trailing newline character - it's a sneaky way to emulate the"echo -n"
command from thebash
shell. It setspasswd
to an empty string as an irrelevant side effect and doesn't wait for input since it's taking its input from thenul:
device.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"
.Setting the delimiters to nothing is required to capture an entire input line with spaces, otherwise you just get the first word. The
"for ... do set ..."
bit setspasswd
to be the actual password output from the VBScript.Then we echo a blank line (to terminate the
"Password: "
line) and the password will be in thepasswd
environment variable after the code has run.Now, as mentioned,
scriptpw.dll
is available only up to XP/2003. In order to rectify this, you can simply copy thescriptpw.dll
file from theWindows\System32
folder of an XP/2003 system to theWinnt\System32
orWindows\System32
folder on your own system. Once the DLL has been copied, you will need to register it by running:To successfully register the DLL on Vista and later, you will need administrator privileges. I haven't examined the legality of such a move so cave lector.
If you're not overly keen on trying to track down and register older DLL files (for convenience or legal reasons), there is another way. Later versions of Windows (the ones that don't have the required DLL) should have Powershell available to you.
And, in fact, 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 ascmd.exe
scripts (such as if you have a lot of code that you don't want to convert), you can use the same trick.First, modify the
cmd
script so it calls Powershell rather than CScript:The Powershell script is equally simple:
although with some marshalling to get the actual password text.
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:
from within Powershell itself.
If you have Python installed, you can use:
the output:
1.Pure batch solution that (ab)uses
XCOPY
command and its/P /L
switches found here (some improvements on this could be found here ):1.2 Another way based on replace command
2.Password submitter that uses a HTA pop-up . This is a hybrit .bat/jscript/mshta file and should be saved as a .bat:
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 the lack of source code bothers you, I have another alternative.
You can get it from http://www.westmesatech.com/misctools.html. Source code is included.
I read all the clunky solutions on the net about how to mask passwords in a batch file, the ones from using a hide.com solution and even the ones that make the text and the background the same color. The hide.com solution works decent, it isn't very secure, and it doesn't work in 64-bit Windows. So anyway, using 100% Microsoft utilities, there is a way!
First, let me explain my use. I have about 20 workstations that auto logon to Windows. They have one shortcut on their desktop - to a clinical application. The machines are locked down, they can't right click, they can't do anything but access the one shortcut on their desktop. Sometimes it is necessary for a technician to kick up some debug applications, browse windows explorer and look at log files without logging the autolog user account off.
So here is what I have done.
Do it however you wish, but I put my two batch files on a network share that the locked down computer has access to.
My solution utilizes 1 main component of Windows - runas. Put a shortcut on the clients to the runas.bat you are about to create. FYI, on my clients I renamed the shortcut for better viewing purposes and changed the icon.
You will need to create two batch files.
I named the batch files runas.bat and Debug Support.bat
runas.bat contains the following code:
You can add as many if "%un%" and if not "%un%" for all the users you want to give access to. The @ping is my coonass way of making a seconds timer.
So that takes care of the first batch file - pretty simple eh?
Here is the code for Debug Support.bat:
I'm not a coder and really just started getting into batch scripting about a year ago, and this round about way that I discovered of masking a password in a batch file is pretty awesome!
I hope to hear that someone other than me is able to get some use out of it!
make a batch file that calls the one needed for invisible characters then make a shortcut for the batch file being called.
right click
properties
colors
text==black
background == black
apply
ok
hope thus helps you!!!!!!!!