Bash script to accept password and replace characters with * as they are typed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to accept password and replace characters with * as they are typed
# 1  
Old 09-27-2011
Bash script to accept password and replace characters with * as they are typed

I googled this and couldn't find an answer, so I rolled my own. Here it is, hope it helps. Feel free to improve on it.

Code:
#!/bin/bash
PWORD=
ANYKEY=0
echo -n "Password:  "
until [ -z "$ANYKEY" ]
do
    read -N 1 -s ANYKEY
    echo -n "*"
    PWORD="$PWORD$ANYKEY"
done
echo
echo $PWORD
exit

# 2  
Old 09-27-2011
Code:
#!/bin/bash

passwd=""
omodes=$(stty -g)

printf "Enter Password: "
while :
do
   stty raw -echo
   c=$(dd bs=1 count=1 2>/dev/null)
   stty -raw

   # break out of loop if CR found
   [[ -z $(echo $c | tr -d "\015") ]] && break

   stty echo
   printf "*"
   passwd=${passwd}${c}
done

stty $omodes

printf "\nPassword entered: $passwd\n"

# 3  
Old 09-27-2011
Sort of cute, couple of thoughts:
1. Use -r option of read to avoid backslash acting as an escape
2. How about support for backspace to allow correction of typos
# 4  
Old 09-28-2011
For the sake of argument, here is an example ksh script called "getpw" that I have in my bag of tricks. I did not write this, but saw it somewhere and saved it. My apologies to the author. Perhaps it can be adapted to bash, or at least studied to learn some new tricks :-)
Code:
#! /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
  n=0
  echo "${prompt}"\\c >&4
  stty -echo -icrnl -icanon min 1 time 0
  reading=1

  while ((reading)) ; do
    keystroke=$(dd bs=1 count=1 2>/dev/null)
    case $keystroke in
          $enter) reading=0
                  ;;
      $backspace) if ((n)) ; then
                    echo "${backspace} ${backspace}"\\c >&4
                    ((n=n-1))
                  fi
                  ;;
               *) echo \*\\c >&4
                  data[n]=$keystroke
                  ((n=n+1))
                  ;;
    esac
  done

  stty "$savesetting"
  echo >&4
  result=""
  i=0

  while ((i<n)) ; do
    result="${result}${data[i]}"
    ((i=i+1))
  done

  echo $result
  return 0
}

final=$(getpass "password: ")
echo the password is $final
exit 0


Last edited by gary_w; 09-28-2011 at 11:31 AM.. Reason: reformatted for readability
These 2 Users Gave Thanks to gary_w For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script for password changes help

I am super new to scripting and I am trying to create a bash script that is interactive that will change other user passwords as well as a few other thing (ie. change SMB pw, see pw expiration,lock/unlock user). I have started it by making it check to see if the username entered is in the... (2 Replies)
Discussion started by: thumbelina
2 Replies

2. How to Post in the The UNIX and Linux Forums

How to replace value of password tag in xml with blanks when special characters are there?

Hi All, I am trying to replace the values inside <password> tag in an xml file but it doesn't replace certain passwords: For eg: Server/home/sperinc>cat TextXML.txt <appIds> <entry name="AccountXref"> <type id="ldap"> <realm>nam</realm> ... (7 Replies)
Discussion started by: saroopkris85
7 Replies

3. Shell Programming and Scripting

Make a password protected bash script resist/refuse “bash -x” when the password is given

I want to give my long scripts to customer. The customer must not be able to read the scripts even if he has the password. The following command locks and unlocks the script but the set +x is simply ignored. The code: read -p 'Script: ' S && C=$S.crypt H='eval "$((dd if=$0 bs=1 skip=//|gpg... (7 Replies)
Discussion started by: frad
7 Replies

4. Shell Programming and Scripting

bash script using scp (pw typed by hand) followed by removal of files

Hello, I tried to write a bash script (code is below) that does scp files that contain a certain string, and that subsequently deletes only those files that have been copied (in my case new files are created every second so it is important to only delete those that have been copied). The key is... (0 Replies)
Discussion started by: kjartan
0 Replies

5. UNIX for Dummies Questions & Answers

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet... (3 Replies)
Discussion started by: Chella15
3 Replies

6. Shell Programming and Scripting

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet able... (0 Replies)
Discussion started by: Chella15
0 Replies

7. Shell Programming and Scripting

Bash script - stripping away characters that can't be used in filenames

I want to create a temp file which is named based on a search string. The search string may contain spaces or characters that aren't supposed to be used in filenames so I want to strip those out. My thought was to use 'tr' with but the result is the opposite of what I want: $ echo "test... (5 Replies)
Discussion started by: mglenney
5 Replies

8. UNIX for Dummies Questions & Answers

ASCII character to accept logon password

Hey all, Just found your forum...Looks super rich with info! Can't wait to get through it all. I am currently writing a web app in .net that telnets into a unix server (require uid + passwd), runs a command, and returns that output to be displayed on the web page. I have gotten through the... (8 Replies)
Discussion started by: gord
8 Replies

9. Shell Programming and Scripting

how to set a variable to accept alpha-numeric characters?

I am working on a shell program that needs to accept alpha-numeric input (i.e., P00375); when I use a simple 'read' statement to read in the input (i.e., read LOG), I receive the message "p00375: bad number". How must I adjust my input statement to accept alpha-numerics? Thanks! Brent (3 Replies)
Discussion started by: bcaunt
3 Replies
Login or Register to Ask a Question