1834629 Members
3223 Online
110069 Solutions
New Discussion

Re: Entering passwords

 
SOLVED
Go to solution
Derek Card
Advisor

Entering passwords

Hello Guys,

I need to prompt the users for passwords without anyone being able to see it? Is there an easy way to do this in a shell script?

TIA, Derek
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Entering passwords

Hi Derek:

Yes, it's quite easy.

#!/usr/bin/sh

trap 'stty echo; exit' 0 1 2 3 15


echo "Enter Password: \c"
stty -echo
read PASSWD
stty echo

I should add that the trap command is really not optional. You see, the stty command alters the tty device, not just your script. Any subsequent processes would also have the echo turned off if you exited in the -echo state. The trap, insures that the terminal is restored upon exit even via a Ctrl-C (or whatever your n'rupt is set to).

I suggest that you comment out the trap statement and do a ctrl-c when entering the password and you will see just what I mean.



Regards, Clay

P.S. You should have been able to find this yourself with that little button.

If it ain't broke, I can fix that.
Derek Card
Advisor

Re: Entering passwords

Thanks, that worked a treat. I would like to add a feature to have the screen display a "*" as each letter is typed.

TIA, Derek
A. Clay Stephenson
Acclaimed Contributor

Re: Entering passwords

That's a bit more difficult and I wouldn't do it from the shell. You have to disable the terminal's line-orieted discipline. It's generally not practical to do character by character entry from the shell. I'm not saying it can't be done but rather the possible negative consequences outweigh the rather limited gains. If I'm going to do this, I use C or Perl (and I don't like to use Perl for this either). From the shell, you can use dd with a bs=1 and count=1 but it's not pretty.
If it ain't broke, I can fix that.
Derek Card
Advisor

Re: Entering passwords

Hello again guys,

I have one last related question. Is there a way in unix to clear the screen? to place the cursor at a certain location? I know I need to echo some kind of character sequence.

TIA, Derek
A. Clay Stephenson
Acclaimed Contributor

Re: Entering passwords

Well, the character sequence is dependent upon the type of terminal. This is set by the TERM variable. When that is set and exported, you can use the "tput" command.

e.g.

tput cup row column
positions the cursor.

tput clear
clears the screen.

Tput looks up these sequences in the terminfo database and instantiates them if the sequence is defined.

Man tput and terminfo for details.

Clay
If it ain't broke, I can fix that.