Link to home
Start Free TrialLog in
Avatar of ghana
ghanaFlag for Germany

asked on

Password input in batch file

Hello experts,

I need to write a batch file that will ask users for account and password and then will connect to different servers using these input data. I can do that with the following code:

REM ******** Begin ************
echo Enter username:
set /p USERID=

echo Enter password:
set /p PWD=

net use I: \\SERVER01\share %PWD% /USER:DOMAIN\%USERID% /PERSISTENT:no
net use J: \\SERVER02\share %PWD% /USER:DOMAIN\%USERID% /PERSISTENT:no
net use K: \\SERVER03\share %PWD% /USER:DOMAIN\%USERID% /PERSISTENT:no

REM ******** End ************

The password will be displayed in clear text on the screen while user input. I want to prevent that the password will be shown on the screen. Because of that I'm looking for a command line tool or any other method that will allow masqueraded user input and will store it in a variable. The batch must be executable on Windows 2000 and XP.

Does anyone have an idea how to do that?

Thanks in advance,
ghana
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

I'll be interested to see if someone comes up with a way; I've never seen one.
Avatar of JamesDS
JamesDS

ghana
Using standard batch commands it isn't possible to enter the password and have it displayed as other than clear text. You have a couple of choices:

Present the script as a web page or convert it to VBScript using an input box.
Replace the net use commands with this:

net use I: \\SERVER01\share * /USER:DOMAIN\%USERID% /PERSISTENT:no
net use J: \\SERVER02\share * /USER:DOMAIN\%USERID% /PERSISTENT:no
net use K: \\SERVER03\share * /USER:DOMAIN\%USERID% /PERSISTENT:no

The net use command itself will ask for the password, which is then not displayed. You will have to remove the bit at the start of the script that asks for a password. The downside of this is that each net use command will probably ask for a password - but if you're lucky it will cache the first one and you'll only get asked once. Only testing will tell you for sure.

Cheers

JamesDS
Avatar of ghana

ASKER

leew, I've never seen one too. That's why I'm asking now.   ;-)

JamesDS, thanks for your comment. One important feature of the script is that it should only aks 1 single time for the username and password. I did already some testing in the past. Because it won't cache the password and does prompt the user for every single connection I need a solution that will store the password in a variable to re-use it in multiple lines.
ghana
In which case VBScript is your answer.

You may be able to specially format an input box, but the preferred method is presenting the whole script within a browser window and use the password input box features of HTML.

Cheers

JamesDS
Avatar of ghana

ASKER

JamesDS, I just want to repeat to find out whether I really understand your suggestion: The job can be done by a VBScript within a HTML page?

Okay, I'm familiar with HTML and the password input box. But I'm not familiar with VB. Do you have an example or do you now a site where I can find those examples. I don't want to learn VBScript at the moment and would be glad if could get a quick solution.
ghana
Try www.15seconds.com

This is an excellent scripting resource and runs a number of mailing lists where the other members are generally very helpful.

This code will connect a drive mapping as a starter:

strConnectionString = "\\"& strFileServer & "\" & strShareName
Set objWSHNetwork = CreateObject("WScript.Network")
objWSHNetwork.MapNetworkDrive strDriveLetter & ":", strConnectionString, True

Cheers

JamesDS
Avatar of ghana

ASKER

After opening a question in the ASP section and discussing the problem there VBScript within a HTML page doesn't seem to be the solution. The problem is that VBScript code is then executed on the web server and not on the client. But the mapping must be done on the client...

Now I start searching again. What I need is something very similar to senvar.com (http://www.desktopian.org/addonsi_cmdln.shtml) but with an additional /pwd switch that will masquerade input on the screen with asteriks.
ghana

I think this link will help you.

http://groups.msn.com/windowsscript/logonscripts.msnw

Cheers

JamesDS
Avatar of ghana

ASKER

Okay, I really found a solution that is working for me. I'm using the Kixtart scripting language to do that. For those who are interested I will post the code below.

Explanation: The user will be prompted to enter his username and password. To prevent that the password is visible on the screen while typing I'm setting both, foreground color and background color, to black (color n/n). Using the 'setl' command I can assign the content of the kix variables to the user defined variables (in this example 'cmduser' and 'cmdpwd'). In the last line a batch is started. In this batch you can now access the user input from the kix script using %cmduser% and %cmdpwd%. Because of security reasons it might be a good idea to add 'set cmdpwd=******' as last line in the batch file. Otherwise the password will stay in the memory in plain text.

If you have any questions, comments, please let me know.

; *************** BEGIN KIX SCRIPT ***************
dim $sUserID         ; account name
dim $sPwd            ; password for account in $sUserID

? "Please enter your user name: "
gets $sUserID
? "Please enter your password: "
color n/n
gets $sPwd
color w/n
setl "cmduser=$sUserID"
setl "cmdpwd=$sPwd"
shell "batchfile.cmd"
; *************** END KIX SCRIPT ***************


REM ************* BEGIN BATCHFILE.CMD *************
REM This is only an example. Displaying cmdpwd in plain text doesn't make sense in a real environment...   ;-)
echo You have entered the following data in the previous kix script:
echo User name=[%cmduser%]
echo Password=[%cmdpwd%]
pause
REM In the last line you should overwrite the plaint text password in the variable cmdpwd
set cmdpwd=*******
REM ************* END BATCHFILE.CMD *************
ghana

Top solution, well done!
Cheers

JamesDS
ASKER CERTIFIED SOLUTION
Avatar of PAQ_Man
PAQ_Man
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial