LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-16-2008, 10:07 AM   #1
dbmacartney
Member
 
Registered: Mar 2007
Location: London, UK
Distribution: Debian, Red Hat Enterprise, Fedora
Posts: 70

Rep: Reputation: 15
Question 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?
 
Old 05-16-2008, 10:50 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Hashes I don't know but with 'read -s' at least they're not echoed.
 
Old 05-16-2008, 12:49 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.

Last edited by colucix; 05-16-2008 at 12:52 PM.
 
1 members found this post helpful.
Old 05-19-2008, 06:55 AM   #4
dbmacartney
Member
 
Registered: Mar 2007
Location: London, UK
Distribution: Debian, Red Hat Enterprise, Fedora
Posts: 70

Original Poster
Rep: Reputation: 15
thanks for the details, thats exactly what I was after. :-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
bash - order of commands when calling remote script? babag Programming 2 04-05-2008 05:10 PM
bash script calling mplayer afrodocter Programming 13 08-23-2005 10:41 AM
mask user input in a bash script PlatinumRik Linux - Software 1 06-15-2004 10:06 AM
User input using a BASH script... causticmtl Programming 5 07-13-2003 09:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration