http://www.perlmonks.org?node_id=33353

reyjrar has asked for the wisdom of the Perl Monks concerning the following question: (input and output)

so, I'm doing the whole talker thing and one thing I've always found annoying about some talkers is that when you are prompted for your passwd, it echoes the letters back to the screen. I know its possible in c to just turn the echo off temporarily, that way you can type your passwd and not have everyone looking over your shoulder see it.
anyone know how to do that in perl?

thanks,
-brad..

Originally posted as a Categorized Question.

  • Comment on How do I make password prompts not echo back the user?

Replies are listed 'Best First'.
Re: How do I make password prompts not echo back the user?
by Shendal (Hermit) on Sep 20, 2000 at 21:47 UTC
    Use Term::Readkey. It is more portable than the unix-only solution of stty. Here's a quick example (untested):
    use Term::ReadKey; print "Type your password:"; ReadMode('noecho'); # don't echo chomp(my $password = <STDIN>); ReadMode(0); # back to normal

    Cheers,
    Shendal
      You might want to try something more like:
      use Term::ReadKey; ReadMode('noecho'); # don't echo print "Type your password:"; chomp(my $password = <STDIN>); ReadMode(0); # back to normal
      ...so you only print the password prompt when echo has already been disabled. This makes your program more compatible with things like Expect, which may not wait long enough after the prompt for the echo to be disabled before sending the password.

      --isotope
      http://www.skylab.org/~isotope/
Re: How do I make password prompts not echo back the user?
by Anonymous Monk on Oct 23, 2003 at 02:30 UTC
    Here's how I did it using Term::Readkey

    ReadMode ('noecho');
    print "Enter Password: ";
    chomp($pw = <STDIN>);
    ReadMode ('restore');
      Another methood is this:
      system('stty -echo'); print "Enter Password: "; chomp($pw = <STDIN>); system('stty echo');

      The computer can't tell you the emotional story. It can give you the exact mathematical design, but what's missing is the eyebrows. - Frank Zappa

         This works but there are two caveats you should bear in mind:

        • If you're running the script as a priviledged process you might have just opened yourself to a local attack, if the user can modify their path and slip in a trojan 'stty' binary.
        • This fails utterly on Windows, amongst other systems.
        Steve
        ---
        steve.org.uk
Re: How do I make password prompts not echo back the user?
by I0 (Priest) on Dec 24, 2000 at 08:30 UTC
    use Term::ReadPassword;
Re: How do I make password prompts not echo back the user?
by Fastolfe (Vicar) on Sep 20, 2000 at 21:31 UTC
    The easiest solution, under Unix at least, is to call 'stty' to change the terminal's "echo" attribute. I know of no way to do this under Windows or any non-Unix platform, and there may be a more standardized way of doing this (such as via curses or some Term-based module):
    print "login: " my $login = <>; print "Password: " system('/usr/bin/stty', '-echo'); # Disable echoing my $password = <>; system('/usr/bin/stty', 'echo'); # Turn it back on
Re: How do I make password prompts not echo back the user?
by strredwolf (Chaplain) on Sep 20, 2000 at 22:52 UTC
    In addition to Term::Readkey, you may want to write up your own linegetter with getc(STDIN). That way, you'll be able to trap all of the deletes/backspaces w/o having any problems.

    Isn't there a Readline module?

A reply falls below the community's threshold of quality. You may see it by logging in.