Stdin Password Reading

Hello fellows!

How can I read a password from stdin without showing it (showing stars
instead, for instance)? I mean as a login program should do, or as passwd
does.

[]s

Pablo

···


Pablo Lorenzzoni (Spectra) spectra@debian.org
GnuPG Key ID 268A084D at search.keyserver.net
Webpage: http://people.debian.org/~spectra/

Hi,

···

At Wed, 28 Aug 2002 06:31:33 +0900, Pablo Lorenzzoni wrote:

How can I read a password from stdin without showing it (showing stars
instead, for instance)? I mean as a login program should do, or as passwd
does.

That question is very system dependent. If your platform has
termios, Termios module may help you.


Nobu Nakada

If I can assume you use a Unix like system and it supports termios
interface, then using termios module like this:

# get current termios value of $stdin.
orig = Termios.getattr($stdin)
tios = orig.dup

# make new value to be set by resetting ECHO and ICANON bit of
# local modes: see termios(4).
tios.c_lflag &= ~(Termios::ECHO|Termios::ICANON)

# set new value of termios for $stdin.
Termios.setattr($stdin, Termios::TCSANOW, tios)

begin
  while c = $stdin.getc  # don't echoing and line buffering
    p c
  end
ensure
  # restore original termios state.
  Termios::setattr($stdin, Termios::TCSANOW, orig)
end

You can use curses extension for this purpose but using it for tiny

problem like this is not preferable.

Or, use extension library interfacing to getpass(3)… I don’t
remember already that is, but believe writing such extension is not so
difficult.

···

In message 200208272131.SAA41799@server.valenciene spectra@debian.org writes:

How can I read a password from stdin without showing it (showing stars
instead, for instance)? I mean as a login program should do, or as passwd
does.


kjana@dm4lab.to August 28, 2002
What can’t be seen is what can’t be there.

Hello!

Thanx. for your tip. You’re right! I use Debian GNU/Linux.

s

Pablo

···

Em Qua 28 Ago 2002 07:36, YANAGAWA Kazuhisa escreveu:

In message 200208272131.SAA41799@server.valenciene > > spectra@debian.org writes:

How can I read a password from stdin without showing it (showing stars
instead, for instance)? I mean as a login program should do, or as passwd
does.

If I can assume you use a Unix like system and it supports termios
interface, then using termios module like this:

# get current termios value of $stdin.
orig = Termios.getattr($stdin)
tios = orig.dup

# make new value to be set by resetting ECHO and ICANON bit of
# local modes: see termios(4).
tios.c_lflag &= ~(Termios::ECHO|Termios::ICANON)

# set new value of termios for $stdin.
Termios.setattr($stdin, Termios::TCSANOW, tios)

begin
  while c = $stdin.getc  # don't echoing and line buffering
    p c
  end
ensure
  # restore original termios state.
  Termios::setattr($stdin, Termios::TCSANOW, orig)
end

You can use curses extension for this purpose but using it for tiny

problem like this is not preferable.

Or, use extension library interfacing to getpass(3)… I don’t
remember already that is, but believe writing such extension is not so
difficult.


Pablo Lorenzzoni (Spectra) spectra@debian.org
GnuPG Key ID 268A084D at search.keyserver.net
Webpage: http://people.debian.org/~spectra/