1833838 Members
2461 Online
110063 Solutions
New Discussion

password for script

 
Nitin Nigam
Occasional Advisor

password for script

Hi,

I have written a script but what I want is whenever someone run the script it will prompt for the password and if its wrong exit the script.
5 REPLIES 5
Rajeev  Shukla
Honored Contributor

Re: password for script

Hi Nitin,
I know "C" and i know that it can solve you'r problem.
Ok here is how you can go
1. read the used id.
2. get the salt characters from /etc/passwd or if trusted system read from tcb files (salt characters are first 2 characters of password)
3. read the password from keyboard and using the salt character you got from previous step, encrypt the password.
4. compare the password either from /etc/passwd or tcb files if trusted system.
If compare results ok then let user run the script or else exit. You can this way even keep log of users who ran the script.
Elmar P. Kolkman
Honored Contributor

Re: password for script

Or do:

su $LOGNAME -c /bin/true
if [ $? -ne 0 ]
then
exit
fi

LOGNAME should contain your login name, so you do a su to yourself.
This works in most cases. Only root doesn't need to enter his own password.
Every problem has at least one solution. Only some solutions are harder to find.
Graham Cameron_1
Honored Contributor

Re: password for script

I guess the first thing you need to do is to prompt for a password and read it in with no echo, and I don't think you can do this purely with shell.

You could write a small C program which would call getpass() and do your validation, returning a success/fail code which your calling script can then check.

Unless maybe perl can do this - I'm no expert here but someone else might be able to help...

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
ALPER ONEY
Advisor

Re: password for script

Hi,
I guess that you would like to validate his/her unix password when this script is run by a unix account. So the most appropriate method seems the method submitted by Elmar P. Kolkman above.
Addition to the information gathered above, I would like to say comments about the reply submitted by Graham Cameron:
a-) If you want avoid showing Password you may read password interactively as
below.


USERNAME="UserName" ## Could be a parameter to your script too.
PASSWORD=""
while [ ! "$PASSWORD" ]
do
echo "\t\t\tEnter ${SERVER} PASSWORD for ${USERNAME}: \c"
stty -echo
read PASSWORD
stty echo
echo ""
done

b-) As far as I know, function getpass() is limited up to 8 characters, so you'd better consider this limitation while writing codes on your platform.
Regards
ALPER ONEY
SYBASE DBA
I.S.E TAKASBANK INC
never ever give up.
Graham Cameron_1
Honored Contributor

Re: password for script

Alper.

You have taught me something there.
You deserve points for that.

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.