LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   calling user input in bash script (https://www.linuxquestions.org/questions/linux-general-1/calling-user-input-in-bash-script-642669/)

dbmacartney 05-16-2008 10:07 AM

calling user input in bash script
 
Hi all

I have written a bash script and basically it asks the user to enter a username and a password.

eg:
echo "please enter username and press [enter]: "
read username
echo "please enter password and press [enter]: "
read password

What I am trying to do is change the characters that come up when the input is typed. in this situation the input from the user comes up as it is typed. eg. username tom shows as tom and password 123456 comes up as 123456. I would like to change the password to come up as hashes instead of the actual password.

has anyone tried to do this?

unSpawn 05-16-2008 10:50 AM

Hashes I don't know but with 'read -s' at least they're not echoed.

colucix 05-16-2008 12:49 PM

You have to change the terminal settings to do this. Using stty you can disable the canonical mode and disable the local echo (this is the same effect of read -s suggested by unSpawn). Then you can detect key strokes using the dd command and put the character corresponding to each key into the password. Echo a single asterisk (or a hash, if you prefer) and repeat until the user hits Enter. Finally restore the original settings and have the "recorded" password at your disposal.
Code:

#!/bin/bash
# ------------------------------------------------------------
#  Prompt user for username and password
# ------------------------------------------------------------

read -p "please enter username and press [enter]: " username
echo -n "please enter password and press [enter]: "
# ------------------------------------------------------------
#  Save current terminal settings
# ------------------------------------------------------------

oldstty=$(stty -g)
# ------------------------------------------------------------
#  Disable canonical mode and local echo
# ------------------------------------------------------------

stty -icanon -echo
# ------------------------------------------------------------
#  Get the first keystroke using dd
# ------------------------------------------------------------

key=$(dd bs=1 count=1 2>/dev/null)
# ------------------------------------------------------------
#  While key is not "Enter" store key in the password,
#  echo an asterisk and get another keystroke
# ------------------------------------------------------------

while [ x$key != x$(echo) ]
do
  password=$password$key
  echo -n \*
  key=$(dd bs=1 count=1 2>/dev/null)
done
echo
# ------------------------------------------------------------
#  Restore old terminal settings
# ------------------------------------------------------------

stty "$oldstty"
# ------------------------------------------------------------
#  Check results
# ------------------------------------------------------------

echo $username
echo $password

How to capture keystrokes using dd is taken from example 15-58 of the Advanced Bash Scripting Guide.

dbmacartney 05-19-2008 06:55 AM

thanks for the details, thats exactly what I was after. :-)


All times are GMT -5. The time now is 08:38 PM.