Add password to batch command?

hello, I'd like a batch command to prompt me for a password when I try to use it, and only work if I type in the correct password, can this be done?
thanks in advance.

Hi, You can use:
read -s -p "password: " pass;echo
to have the entered password stored in the variable pass.

A few hints:
Anyone who can edit the script can also change it to for example echo $pass, a security risk...
create an encrypted passwordfile, for example with
echo lakris $(echo secretpassword | md5sum ) >> .mypasswd
Make sure the passwd file is only readable by the owner of the script
Test the entered and encrypted $pass against what's in the file for a given user.

It would be a very rudimentary password handling routine but this should get You started!

/Lakris

yikes, your way over my head buddy!?!? perhaps you could give an example. how would I alter the following batch command to make it prompt me for a password to execute it.

@echo off
color 70
if exist "c:\new folder\file.txt" del "c:\new folder\file.txt"
ECHO hi everybody
ECHO.
PAUSE

Sorry, I didnt realise this was a Windows forum, how silly of me!

I wouldn't be able to do it in cmd.exe. Maybe someone else. But I can recommend ss64.com, it has a very good description of a lot of cmd.exe commands and options.

/Lakris

thanks anyway, I'll take a look at that website.

meanwhile I'd still greatly appreciate any help from anyone

Well, the following is not really a "password" facility in the normal sense - but as far as i know the best you can get in pure DOS without resorting to Cygwin or the like:

  1. switch off screen echo so nobody sees what is going on
  2. load a commando processor with an input redirection to the keyboard
  3. switch back on normal output.

Note that step 3 would only occur after leaving the loaded commando shell, so the "password" is fixed - to "exit[<enter>]". The following should work in real DOS-versions but probably not in WinXP DOS-boxes or the like:

@ctty nul
command.com < con:
@ctty con:
@echo off
echo here we are again

It has been a long time since i last used DOS and i am not sure if it is "con" or "con:", so you have to find that out. IIRC the colon was the sign of a character oriented device, so "con:" should be correct but it might well be the other way round and devices with colons are block-oriented.

I hope this helps.

bakunin

note to bakunin

what you said gave me a headache, I'm really really newbie so you'll have to talk down even more :slight_smile:

jk, I figured out a solution that works great here it is for reference

@echo off
set "p="
set /p "p=Type in the password please... "
if not "%p%"=="my password" goto :EOF

NOTE: if you make your own batch command with this just change the part that says "my password" to what you want your password to be, remember to keep the quotes.

Hello Killalot57,

I guess youa re looking for this kind of solution...

@echo off
set /p password="please enter the password:"
echo %password%

pause
color 70
if exist "c:\new folder\file.txt" del "c:\new folder\file.txt"
ECHO hi everybody
ECHO.
PAUSE

you can make the script to ask for any variable with the above..! may be you can take a look at runas command and integrate it with If command to authenticate; you should have the permissions on the password file as appropriate.

--ilan