No echo

How to get string from console without echo? I need it for password reading.

Szymon Drejewicz

EHLO

  • On 2003-03-11 13:57

How to get string from console without echo? I need it for password
reading.

Use termios available at

http://raa.ruby-lang.org/list.rhtml?name=ruby-termios

Sample usage (note that the input routine has the potential of
showing a strange behavior: When a star is shown for every password
character, the stars do not fit into one line and you use backspace
the stars in the first line will not vanish even if the corresponding
characters are deleted):

require ‘termios’ # Use termios for non-echoing input of password

BS = 8
LF = 10
DEL = 127

···

#==============================================================================

Prompt for and reads in a password. When `stars’ is true display a star for

each character when the password is entered, otherwise do not provide any

visual feedback. BS (Backspace) and DEL (Delete or rubout) can be used to

edit input.

#==============================================================================
def read_pass(stars)
pass = ‘’

$stdin.extend Termios
oldt = $stdin.tcgetattr
newt = oldt.dup
newt.lflag &= ~(Termios::ECHO | Termios::ICANON)
$stdin.tcsetattr(Termios::TCSANOW, newt)

loop {
c = $stdin.getc
break if c == LF
if (c == BS or c == DEL) and (not pass.empty?)
pass.chop!
print “#{BS.chr} #{BS.chr}” if stars
else
pass <<= c.chr
print ‘*’ if stars
end
}

$stdin.tcsetattr(Termios::TCSANOW, oldt)
print “\n”

return pass
end

HTH

Josef ‘Jupp’ Schugt

.---------------------. .------…—…-----…—…-----.

http://jupp.tux.nu/ | | jupp || @ || gmx || . || net |
---------------------' ------‘---'-----’---'-----’

Thanks for the demonstration, Josef. I often wondered myself how to
do it. Your program is archived at

http://www.rubygarden.org/ruby?PasswordWithoutEcho

Gavin

···

On Wednesday, March 12, 2003, 2:08:57 AM, Josef wrote:

EHLO

How to get string from console without echo? I need it for password
reading.

Use termios available at …