Operating System - HP-UX
1820635 Members
1777 Online
109626 Solutions
New Discussion юеВ

How to hide password using shell script

 
SOLVED
Go to solution
Catherine_3
Occasional Advisor

How to hide password using shell script

Hi everyone,
I met a problem
I have a shell script:

echo " input Host name "
read HOST
echo "input User Name "
read USERID
echo " input Password"
read PASSWD

ftp -n -i $HOST <<- EOF
user $USERID $PASSWD
..
..
EOF

I want to hide my password and don't appear on
the screen.

Thanks a lot
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to hide password using shell script

#!/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, insure 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

If it ain't broke, I can fix that.
S.K. Chan
Honored Contributor

Re: How to hide password using shell script

You would put ..

stty -echo

before the "read password" part
and

stty echo

after that.
Catherine_3
Occasional Advisor

Re: How to hide password using shell script

Hi clay,

I had a test. You are right.

thanks clay and chan
Catherine_3
Occasional Advisor

Re: How to hide password using shell script

Hi

By the way, I just want to make the cursor move and display "******"
so How to do that ?
A. Clay Stephenson
Acclaimed Contributor

Re: How to hide password using shell script

That is much more difficult because you have to turn off line-buffered entry and enable character by character input. That is something generally not done from the shell.
You can use the command 'tput cup row column' to position the cursor. Man tput, terminfo for details.

If it ain't broke, I can fix that.
Catherine_3
Occasional Advisor

Re: How to hide password using shell script

Thanks a lot Clay