Gary Yang wrote:
> Hi,
>
> The script bellow prompt user to enter login password. But, when I enter
> the passwd at prompt, it shows me the passwd I typed. How to make the
> passwd invisible while I'm typing the passwd?
>
> print "\nPlease Enter login passwd: ";
> chomp($passwd = <STDIN>);
Try something like this (on Windoze, it takes like 4 returns for ReadKey to get
the return, but should work fine on Linux) - this one allows editing of the
passwd
during input (<BS>/^U) :
#!perl --
use strict;
use warnings;
use Term::ReadKey; END { ReadMode ('restore'); } # just in case
my $passwd = get_passwd ();
print "Normally you wouldn't print this: $passwd\n";
exit;
sub get_passwd {
# legal clear passwd chrs (26+26+10+24=86): "a-zA-Z0-9!#$%&()*+,-./:;<=>[EMAIL
PROTECTED]";
my @legal_clear = ('a'..'z', 'A'..'Z', '0'..'9', split //,
'!#$%&()*+,-./:;<=>[EMAIL PROTECTED]');
my %legal_clear; foreach (@legal_clear) { $legal_clear{$_} = 1; }
$| = 1; # unbuffer stdout to force unterminated line out
print "Password: ";
ReadMode ('cbreak');
my $ch = '';
while (defined ($ch = ReadKey ())) {
last if $ch eq "\x0D" or $ch eq "\x0A";
if ($ch eq "\x08") { # backspace
print "\b \b" if $passwd; # back up 1
chop $passwd;
next;
}
if ($ch eq "\x15") { # ^U
print "\b \b" x length $passwd; # back 1 for each char
$passwd = '';
next;
}
if (not exists $legal_clear{$ch}) {
print "\n'$ch' not a legal password character\n";
print 'Password: ', "*" x length $passwd; # retype *'s
next;
}
$passwd .= $ch;
print '*';
}
print "\n";
ReadMode ('restore');
return $passwd;
}
__END__
_______________________________________________
Perl-Unix-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs