Encrypt password but use * when typing password

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Encrypt password but use * when typing password
# 1  
Old 06-30-2011
Encrypt password but use * when typing password


I came across the following script for encrypting the password in this forum
#! /usr/bin/ksh
exec 4>/dev/tty
function getpass
        typeset prompt=$1
        typeset backspace=$(echo  \\b\\c)
        typeset enter=$(echo \\r\\c)
        typeset savesetting=$(stty -g)
        typeset keystroke password n i reading result
        echo "${prompt}"\\c >&4
        stty -echo -icrnl -icanon min 1 time 0
        while ((reading)) ; do
                keystroke=$(dd bs=1 count=1 2>/dev/null)
                case $keystroke in
                        if ((n)) ; then
                                echo "${backspace} ${backspace}"\\c >&4
                        echo \*\\c >&4
        stty "$savesetting"
        echo >&4
        while  ((i<n)) ; do
        echo $result
        return 0
final=$(getpass "password:")
echo the password is $final
exit 0

I copied the script and executed it. I executed in RHEL. But this script outputs as given below:

[root@netcool ncoadmin]# ./a.sh

I was expecting I should get something like this

password: ********

meaning whatever the password I type it should be in asterix. But this script keeps on moving down when I press a single character.

Can someone help me in this regard.

# 2  
Old 06-30-2011
echo adds a newline, and doesn't understand escape sequences, try printf, which does understand escape sequences and doesn't add newlines.

I have no idea what \c is intended to do, though. That's not a valid escape sequence.
# 3  
Old 06-30-2011
I've used \c in the past on AIX.

From man echo on AIX:

            Suppresses the new-line character that otherwise follows the final argument in the output. All characters following the \c sequence are ignored.

(of course now, I'd use printf!)
# 4  
Old 06-30-2011
Worked for me

Your script worked for me on Solaris exactly as copied:
$ getpw
the password is efs
$ getpw
the password is thisisatest

the \c means suppress the newline after printing the string.

I would put the escape sequence inside the quotes though so you don't need to worry about escaping the escape sequence which is where I think your issue is.
echo "testing\c"

Last edited by gary_w; 06-30-2011 at 02:05 PM..
# 5  
Old 06-30-2011
Hi All,

Thanks for your replies.

I tried this on AIX, Solaris yes this works fine.

But on RHEL this does not work it outputs as I shown in my first post.

Any idea what should I do in RHEL, how to overcome this?

# 6  
Old 06-30-2011
Can't help you there, but...

You should also use the trap command to reset the stty settings if the script is interrupted while running. Otherwise, your terminal session will be left hosed if you interrupt it while it is waiting for input.

trap 'stty "$savesetting"' 2 3 9 15

the command in the single quotes will be executed if any of those signals are received. Some common signals:

SIGHUP (1) Hang up.
SIGINT (2) Interrupt from keyboard. Issued if you press ^C.
SIGQUIT (3) Quit from keyboard. Issued if you press ^D.
SIGFPE (8) Floating point exception.
SIGKILL (9) Kill signal.
SIGTERM (15) Terminate. Cause the program to quit gracefully
SIGCHLD (17) Child terminate.
# 7  
Old 06-30-2011
Try echo -e, Linux won't understand \c without it. Or echo -n without the \c, to tell echo not to print a newline.

I really do think printf would be better, since newlines (or the lack of them) are a standard, portable feature of it -- should work the same on UNIX or Linux, shell builtin or external.

$ printf "hello\n"
$ printf "hello"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Encrypt and Decrypt a File with Password

Hello, I have few files on unix which are payroll related and I need them to encrypt with password so others wouldn't see the data. I use ETL tool and would like to know the unix command that does encryption/decryption to use in the ETL. Thank you, Sri (3 Replies)
Discussion started by: eskay
3 Replies

2. Shell Programming and Scripting

Encrypt Plaintext Password?

How do I encrypt a plaintext password that I need hardcoded in a bash script? (3 Replies)
Discussion started by: wyclef
3 Replies

3. Shell Programming and Scripting

Encrypt DB password in Script

Hi, I have a SQL which i want to run through a shell script. query_result=`/home/oracle/product/11.2.0/bin/sqlplus -S uname/pwd@DBNAME <<! set heading off feedback off trimspool on set pagesize 0 set linesize 9999 spool $PARAM_PATH/param_name.txt; Select sysdate from dual; spool off;... (6 Replies)
Discussion started by: chetan.c
6 Replies

4. AIX

SSH session closes after typing correct password

hi guys need some help. when ever i'm login ssh to aix server session always closed. when trying t0 type wrong password the session still continues, but we tried the correct password it automatically ends. what could be the problem to this please see .profile details ... (6 Replies)
Discussion started by: bocha
6 Replies

5. Shell Programming and Scripting

How to Encrypt password

Hello, I have a paramter file, In which I store all the user-ids and passwords for the project. So if a user just invokes the paramter file he has access to all the variables, which i have exported in the parmatere file. Now if a user echo's the variable which stores the databse password.... (1 Reply)
Discussion started by: DSDexter
1 Replies

6. Shell Programming and Scripting

Encrypt and save password

Hello all, I need to encrypt and save a password in a file and later decrypt to determine the original password string. Please suggest how can I use the linux commands or other approach to accomplish this. Thanks for your help. (1 Reply)
Discussion started by: cheerful
1 Replies

7. Shell Programming and Scripting

cronjob on a remote ssh without typing password

Hi there, How can I send a script to a remote ssh client. 1) I cannot connect through ftp 2) I just need to read a file on a remote server. Here is what I do: ~$ ssh santiago@myserver.com santiago@myserver.com's password: santiago@myserver:~$ cat logfile hello world bonjour le monde hola... (1 Reply)
Discussion started by: chebarbudo
1 Replies

8. UNIX and Linux Applications

How to encrypt oracle password table ?

Hi Oracle & Unix expert, Could you tell me how to encrypt oracle table ? Thanks. (1 Reply)
Discussion started by: olaris
1 Replies

9. Shell Programming and Scripting

encrypt and decrypt password

how do i encrypt and decrypt a password (2 Replies)
Discussion started by: sanwish
2 Replies

10. Programming

hide password typing

I am doing a project in C program which requires to type in password in Unix terminal. Does anybody know how to shade or not output any words typed by user in the terminal? I use the function scan() to read typing from user. Thanks in advance. (2 Replies)
Discussion started by: ivancheung
2 Replies
Login or Register to Ask a Question