Jump to content

Simple CMD Batch Script : Prompt for Password and hide it?


Recommended Posts

I am sure this will be very easy for you clever scripting guys out there, here's a couple of questions.

I have wrote this little script for some wireless laptops that we are not going to join our domain, but we'd like our students to access to thier home drives, and shared resources.

===============================

echo off

net use z: /delete

net use t: /delete

echo Please type your Year Group below and press enter (7,8,9,10 or 11)

set /p Group=

if "%Group%" == "7" set intake=2008

if "%Group%" == "8" set intake=2007

if "%Group%" == "9" set intake=2006

if "%Group%" == "10" set intake=2005

if "%Group%" == "11" set intake=2004

echo Please Type your Username (example astudent120589)

set /p usern=

echo Now please Type your Password

set /p password=

net use Z: "\\Server\User Storage\Pupils\%intake%\%usern%" /User:mydomain\%usern% %password% /PERSISTENT:NO

net use T: "\\Server\student$" /User:mydomain\%usern% %password% /PERSISTENT:NO

===============================

1. It works but i'd prefer to hide the password as they type it? is it possible?

2. Also is it possible to add a line that to set the path of MY documents to Z: drive,

so applications trying to save will pick z: as it's default location.

Any help or suggestions on how to improve this script would be great.

Thank you.

Steve, Chaucer BEC, Sheffield UK

www.chaucer.sheffield.sch.uk

Link to comment
Share on other sites


echo off
net use z: /delete
net use t: /delete

echo Please type your Year Group below and press enter (7,8,9,10 or 11)
set /p Group=
if "%Group%" == "7" set intake=2008
if "%Group%" == "8" set intake=2007
if "%Group%" == "9" set intake=2006
if "%Group%" == "10" set intake=2005
if "%Group%" == "11" set intake=2004

echo Please Type your Username (example astudent120589)
set /p usern=

echo Now please Type your Password
set /p password=

net use Z: "\\Server\User Storage\Pupils\%intake%\%usern%" /User:mydomain\%usern% %password% /PERSISTENT:NO
net use T: "\\Server\student$" /User:mydomain\%usern% %password% /PERSISTENT:NO

For now I think you should ignore the password visibility issue and look at your code structure. The first thing I notice is that you go straight into deleting two drive mappings, you have not checked to see if they exist already, or even if they are already mapped to the intended destinations. If they are already mapped to another location you may need to make a note of that location and re-map them to an alternative letter. Then you ask for user input, which I suggest should be your first task, but fail to check any of the input. If the user types 5 for their Year Group your code would continue needlessly with an invalid figure, the same could be said for Username, ideally it needs to be checked for validity.

As far as the hiding of password entry is concerned, I'd suggest you use vbscript / html|hta solution, which could either be directly scripted or created and run directly from the batch file.

As a final thought, due to possible limitations in your chosen scripting method, you may need to also to ensure that invalid / poison characters are not used, especially with the password input.

Link to comment
Share on other sites

Here a example of a HTA with a masked textbox

 <TITLE>Submit Password</TITLE>
<HTA:APPLICATION ID='PWSubmit'
Scroll='No'
SCROLLFLAT ='No'
SingleInstance='Yes'
SysMenu='Yes'
ShowInTaskBar='No'
MaximizeButton='No'
MinimizeButton='No'
Border='Thin'
BORDERSTYLE ='complex'
INNERBORDER ='Yes'
Caption='Yes'
WindowState='Normal'
APPLICATIONNAME='SubmitPW'
Icon='%SystemRoot%\explorer.exe'>
<STYLE Type="text/css">
Body
{
Font-Size:8.05pt;
Font-Weight:Bold;
Font-Family:Arial,Tahoma,Comic Sans MS,Segoe Ui;
Color:#001254;
BackGround-Color:Transparent;
Filter:progid:DXImageTransform.Microsoft.Gradient
(StartColorStr='#fdf7f1',endColorStr='#d1cbc5');
Margin-Top:1;
Margin-Bottom:1;
Margin-Left:4;
Margin-Right:4;
Padding-Top:3;
Padding-Bottom:3;
Padding-Left:4;
Padding-Right:4;
Text-Align:Center;
Vertical-Align:Top;
Border-Top:2px Solid #a6a29e;
Border-Bottom:3px Solid #cbc7c3;
Border-Left:2px Solid #b2aeaa;
Border-Right:3px Solid #bcb8b4;
}
BUTTON
{
Width:51PT;
Height:15PT;
Cursor:Hand;
Font-Size:7.95pt;
Font-Weight:Bold;
filter:progid:DXImageTransform.Microsoft.Gradient
(StartColorStr='#FEFDFC',endColorStr='#D7D9D7');
Margin-Top:1;
Margin-Bottom:1;
Margin-Left:3;
Margin-Right:3;
Padding-Top:1;
Padding-Bottom:1;
Border-left: 1px Ridge;
Border-right: 1px Groove #bcb8b4;
Border-top: 1px Ridge;
Border-Bottom: 1px Groove #bcb8b4;
}
.TxBox
{
Height:15PT;
Font-Size:7.95pt;
Font-Weight:Bold;
Font-Family:Arial,Tahoma,Comic Sans MS,Segoe Ui;
Color:#1e1e1e;
filter:progid:DXImageTransform.Microsoft.Gradient
(StartColorStr='#FEFDFC',endColorStr='#D7D9D7');
Margin-Top:1;
Margin-Bottom:1;
Margin-Left:3;
Margin-Right:3;
Padding-Top:1;
Padding-Bottom:1;
Border-left: 1px Ridge;
Border-right: 1px Groove #bcb8b4;
Border-top: 1px Ridge;
Border-Bottom: 1px Groove #bcb8b4;
}
</STYLE>
<script language="VBScript">
'-> Resize Window
Dim Wth :Wth = int(349)
Dim Hht :Hht = int(125)
window.ResizeTo Wth, Hht
MoveTo ((Screen.Width / 2) - (Wth / 2)),((Screen.Height / 2) - (Hht / 2))
'-> Btn01 Function
Function PwAction()
If TB1.value = "" Then
alert("Error No Password")
Else
alert(TB1.value)
End If
End Function
'-> Btn02 Function
Function CloseAction()
window.close()
End Function
</SCRIPT>
<BODY Scroll='No'>
<!-- Text -->
<DIV Style='Margin-Top:3pt;Margin-Bottom:5pt;'>Type in your password, then press the Submit Button.</DIV>
<!-- Password Textbox -->
<TABLE>
<INPUT Type='Password' Class='TxBox' Name='TB1' SIZE=18 MAXLENGTH=18>
</TABLE>
<!-- Buttons -->
<TABLE>
<BUTTON ID='Btn01' OnClick='PwAction()'>Submit</BUTTON>
<BUTTON ID='Btn02' OnClick='CloseAction()'>Close</BUTTON>
</TABLE>

Link to comment
Share on other sites

and here's a batch file which writes a vbs and uses a html for obtaining the data.

@Echo off
>%TEMP%\_$.vbs (Echo/wsh.echo "You entered: ", _
Echo/ join^(passwordbox^("Enter UID and password", "Testing"^), ", "^)
Echo/
Echo/' A function to present a Password dialog in a VBS ^(WSF^) script
Echo/' Requires WScript version 5.1^+
Echo/' Tom Lavedas ^<tlave...@hotmail.com^>
Echo/Function PasswordBox^(sPrompt, sDefault^)
Echo/ set oIE = CreateObject^("InternetExplorer.Application"^)
Echo/ With oIE
Echo/ .ToolBar = False
Echo/ .RegisterAsDropTarget = False : .Navigate^("about:blank"^)
Echo/ While .Busy : WScript.Sleep 100 : Wend
Echo/ With .document
Echo/ With .ParentWindow
Echo/ if Instr^(.navigator.appVersion, "MSIE 6"^) = 0 Then
Echo/ oIE.FullScreen = True
Echo/ .resizeto 400,180
Echo/ .moveto .screen.width/2-200, .screen.height/2-90
Echo/ else
Echo/ .resizeto 400,230
Echo/ .moveto .screen.width/2-200, .screen.height/2-115
Echo/ End if
Echo/ End With
Echo/ .Write^("<html><head><" ^& "script>bboxwait=true;</" ^& "script>" _
Echo/ ^& "<title>Password _____________________________ </title>" _
Echo/ ^& "</head><body bgColor=Silver scroll=no language=vbs" _
Echo/ ^& " onkeypress=""if window.event.keycode=13 Then" _
Echo/ ^& " bboxwait=false""><center><b> " ^& sPrompt ^& "<b> <p>" _
Echo/ ^& "<table><tr><td> <b>User:</b></td><td>" _
Echo/ ^& "<input type=text id=user value='" ^& sDefault ^& "'>" _
Echo/ ^& "</td><tr><td> <b>Password:</b></td><td>" _
Echo/ ^& "<input type=password id=pass></td></tr></table><br>" _
Echo/ ^& "<button onclick=""bboxwait=false;""> Submit </button>" _
Echo/ ^& "</center></body></html>"^)
Echo/ .ParentWindow.document.body.style.borderStyle = "outset"
Echo/ .ParentWindow.document.body.style.borderWidth = "3px"
Echo/ .all.user.focus
Echo/ On Error Resume Next
Echo/ Do While .parentWindow.bBoxWait
Echo/ oIE.Visible = True
Echo/ if Err Then Exit Do
Echo/ WScript.Sleep 100
Echo/ Loop
Echo/ oIE.Visible = False
Echo/ if Err Then
Echo/ PasswordBox = Array^("CANCELLED"^)
Echo/ Else
Echo/ PasswordBox = Split^(.all.user.value ^& "|" _
Echo/ ^& .all.pass.value, "|"^)
Echo/ End if
Echo/ On Error Goto 0
Echo/ End With ' document
Echo/ End With ' IE
Echo/End Function)
Cscript //NoLogo %TEMP%\_$.vbs
Del %TEMP%\_$.vbs
Pause

Link to comment
Share on other sites

gunsmokingman.

Thank for your reply, I can't see how a web page can control drive mapping?

Yzowl.

Thank for your reply, but i just need to hide the password, so it's looks like I need to go down the VBS route.

I am not that bother about error checking the batch file, if they type something wrong, the script fails and they don't get connected.

I have tried the code in a batch file, but I got this error.

Any further suggestions?

Thank you for your help.

Steve


E:\>{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\froman\fcharset0 Time
s New Roman;}{\f1\fswiss\fcharset0 Arial;}}
The system cannot find the path specified.

E:\>{\*\generator Msftedit 5.41.21.2508;}\viewkind4\uc1\pard\sb100\sa100\f0\fs24
@Echo off\line (Echo/wsh.echo "You entered: ", _\line Echo/ join(passwordbox("
Enter UID and password", "Testing"), ", ")\line Echo/\line Echo/' A function to
present a Password dialog in a VBS (WSF) script\line Echo/' Requires WScript ver
sion 5.1+\line Echo/' Tom Lavedas <tlave...@hotmail.com>\line Echo/Function Pass
wordBox(sPrompt, sDefault)\line Echo/ set oIE = CreateObject("InternetExplorer.A
pplication")\line Echo/ With oIE\line Echo/ .ToolBar = False\line Echo/ .Registe
rAsDropTarget = False : .Navigate("about:blank")\line Echo/ While .Busy : WScrip
t.Sleep 100 : Wend\line Echo/ With .document\line Echo/ With .ParentWindow\line
Echo/ if Instr(.navigator.appVersion, "MSIE 6") = 0 Then\line Echo/ oIE.FullScre
en = True\line Echo/ .resizeto 400,180\line Echo/ .moveto .screen.width/2-200, .
screen.height/2-90\line Echo/ else\line Echo/ .resizeto 400,230\line Echo/ .move
to .screen.width/2-200, .screen.height/2-115\line Echo/ End if\line Echo/ End Wi
th\line Echo/ .Write("<html><head><" & "script>bboxwait=true;</" & "script>" _\l
ine Echo/ & "<title>Password _____________________________ </title>" _\line Echo
/ & "</head><body bgColor=Silver scroll=no language=vbs" _\line Echo/ & " onkeyp
ress=""if window.event.keycode=13 Then" _\line Echo/ & " bboxwait=false""><cente
r><b> " & sPrompt & "<b> <p>" _\line Echo/ & "<table><tr><td> <b>User:</b></td><
td>" _\line Echo/ & "<input type=text id=user value='" & sDefault & "'>" _\line
Echo/ & "</td><tr><td> <b>Password:</b></td><td>" _\line Echo/ & "<input type=pa
ssword id=pass></td></tr></table><br>" _\line Echo/ & "<button onclick=""bboxwai
t=false;""> Submit </button>" _\line Echo/ & "</center></body></html>")\line Ech
o/ .ParentWindow.document.body.style.borderStyle = "outset"\line Echo/ .ParentWi
ndow.document.body.style.borderWidth = "3px"\line Echo/ .all.user.focus\line Ech
o/ On Error Resume Next\line Echo/ Do While .parentWindow.bBoxWait\line Echo/ oI
E.Visible = True\line Echo/ if Err Then Exit Do\line Echo/ WScript.Sleep 100\lin
e Echo/ Loop\line Echo/ oIE.Visible = False\line Echo/ if Err Then\line Echo/ Pa
sswordBox = Array("CANCELLED")\line Echo/ Else\line Echo/ PasswordBox = Split(.a
ll.user.value & "|" _\line Echo/ & .all.pass.value, "|")\line Echo/ End if\line
Echo/ On Error Goto 0\line Echo/ End With ' document\line Echo/ End With ' IE\li
ne Echo/End Function)\line Cscript //NoLogo c:\Windows\Temp\\_$.vbs\line Del c:\
Windows\Temp\\_$.vbs\line Pause\par 1>c:\Windows\Temp\\_$.vbs
The system cannot find the path specified.

E:\>\pard\f1\fs20\par
The system cannot find the path specified.

E:\>}
'}' is not recognized as an internal or external command,
operable program or batch file.

Link to comment
Share on other sites

Why not just let the Net Use command collect the password? Granted it is not the prettiest, but it doesn't echo anything to the screen.

net use n: \\myserver\mystorage /user:domain\user

Then it asks for the password for the specified user to connect with.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...