in reply to Accepting Password from user

Here is a subroutine for just that purpose. In this case, it asks for a password twice and returns undef if they didn't match.

sub get_passwd_on_console { use POSIX; print "Password: "; # Set the terminal to "no echo" mode for reading the passphrase my ($term, $oterm, $echo, $noecho, $fd_stdin); $fd_stdin = fileno(STDIN); $term = POSIX::Termios->new; $term->getattr($fd_stdin); $oterm = $term->getlflag(); $echo = ECHO | ECHOK | ICANON; $noecho = $oterm & ~$echo; $term->setlflag($noecho); $term->setattr($fd_stdin, TCSANOW); my $passwd = <>; chomp $passwd; print "\n"; print "Password (retype): "; my $passwd2 = <>; chomp $passwd2; print "\n"; # Set the terminal back $term->setlflag($echo); $term->setattr($fd_stdin, TCSANOW); return ($passwd eq $passwd2) ? $passwd : undef; }

Replies are listed 'Best First'.
Re: Re: Accepting Password from user
by TheYoungMonk (Sexton) on Jan 27, 2003 at 05:27 UTC
    Thanks for all ur suggestions...unfortunately my problem remains unsolved!!

    Currently I need to use Perl 5.6 on a Windows 2000 machine. For the code given by hardburn, it says Posix::termios not supported!

    i am unable to use CPAN module termkey and not able to install it using CPAN or PPM...Maybe because I'm not using ActiveState perl

    So..is there any other suggestions which can be used to solve this problem of accepting user password without printing on console ?? If perl 5.6 doesnt provide any ways by default, anyother subroutine code that'll do the job of accepting pswd without console display ?

    Still awaiting enlightening suggestions from the monks...