Wie kann ich ein Passwort mit Perl eingeben und die Zeichen mit ‚*‘ ersetzen?
Frage
Ich habe einen Perl-Skript, das den Benutzer erfordert ein Passwort einzugeben. Wie kann ich echo nur ‚*‘ anstelle des Charakters, dass die Benutzertypen, wie sie es geben?
Ich verwende Windows XP / Vista.
Lösung
Sie können mit dem Begriff spielen :: ReadKey. Hier ist ein sehr einfaches Beispiel, mit einiger Erkennung für Backspace und Schlüssel löschen. Ich habe es auf Mac OS X 10.5 getestet, aber nach dem ReadKey Handbuch es sollte unter Windows arbeiten. Die Handbuch zeigt an, dass unter Windows nicht blockierend mit liest (ReadKey(-1)
) fehl. Deshalb verwende ich ReadKey (0), die im Grunde getc
ist (mehr getc in der libc Handbuch ).
#!/usr/bin/perl
use strict;
use warnings;
use Term::ReadKey;
my $key = 0;
my $password = "";
print "\nPlease input your password: ";
# Start reading the keys
ReadMode(4); #Disable the control keys
while(ord($key = ReadKey(0)) != 10)
# This will continue until the Enter key is pressed (decimal value of 10)
{
# For all value of ord($key) see http://www.asciitable.com/
if(ord($key) == 127 || ord($key) == 8) {
# DEL/Backspace was pressed
#1. Remove the last char from the password
chop($password);
#2 move the cursor back by one, print a blank character, move the cursor back by one
print "\b \b";
} elsif(ord($key) < 32) {
# Do nothing with these control characters
} else {
$password = $password.$key;
print "*(".ord($key).")";
}
}
ReadMode(0); #Reset the terminal once we are done
print "\n\nYour super secret password is: $password\n";
Andere Tipps
In der Vergangenheit habe ich verwendet IO :: Prompt für diese.
use IO::Prompt;
my $password = prompt('Password:', -e => '*');
print "$password\n";
Wenn Sie nicht möchten, dass alle Pakete verwenden ... Nur für UNIX
system('stty','-echo');
chop($password=<STDIN>);
system('stty','echo');
Sie sollten einen Blick auf entweder Term :: ReadKey oder Win32 :: Console . Sie können diese Module verwenden, um die einzelnen Tastenanschläge zu lesen und ‚*‘ oder whathever emittieren.
Aufbauend auf Pierr-Luc Programm, haben soeben eine gewisse Kontrolle über die Schrägstriche. Damit können Sie nicht drücken Backslash halten für immer:
sub passwordDisplay() {
my $password = "";
# Start reading the keys
ReadMode(4); #Disable the control keys
my $count = 0;
while(ord($key = ReadKey(0)) != 10) {
# This will continue until the Enter key is pressed (decimal value of 10)
# For all value of ord($key) see http://www.asciitable.com/
if(ord($key) == 127 || ord($key) == 8) {
# DEL/Backspace was pressed
if ($count > 0) {
$count--;
#1. Remove the last char from the password
chop($password);
#2 move the cursor back by one, print a blank character, move the cursor back by one
print "\b \b";
}
}
elsif(ord($key) >= 32) {
$count++;
$password = $password.$key;
print "*";
}
}
ReadMode(0); #Reset the terminal once we are done
return $password;
}
Pierr-Luc Programm mit
# Start reading the keys
ReadMode(4); #Disable the control keys
while(ord($key = ReadKey(0)) != '13' )
# This will continue until the Enter key is pressed (decimal value of 10)
{
# For all value of ord($key) see http://www.asciitable.com/
if(ord($key) == 127 || ord($key) == 8 && (length($password) > 0)) {
# DEL/Backspace was pressed
#1. Remove the last char from the password
chop($password);
#2 move the cursor back by one, print a blank character, move the cursor back by one
print "\b \b";
} elsif(ord($key) > 32) {
$password = $password.$key;
print "*";
}
}
ReadMode(0); #Reset the terminal once we are done
Haben Sie versucht, die Zeichenfolge zu speichern (so dass Ihr Programm noch lesen kann) und seine Länge herausfinden, dann eine Reihe von gleicher Länge erstellen, sondern nur ‚*‘ verwenden?