perl - Perl 如何在不显示终端的情况下提示输入密码?

标签 perl terminal passwords

如何提示终端用户输入密码而不在终端中显示密码? How can I enter a password using Perl and replace the characters with '*'? 有一个类似的问题,但答案似乎已过时或已损坏。

我一直在寻找这个问题的答案,并且对后面几页结果中的问题不满意,这些问题的答案与其他问题无关。

另外,perlfaq8有一个不完整的例子(哦,谁曾经维护过;)。

 use Term::ReadKey;
 ReadMode('noecho');
 my $password = ReadLine(0);

当您在不重置终端的情况下执行此操作时,没有其他任何回应!哎呀!

我也在寻找更新的答案或模块,我们可以提供给想要这样做的人。

最佳答案

使用 Term::ReadKey ,这有效:

sub prompt_for_password {
    require Term::ReadKey;

    # Tell the terminal not to show the typed chars
    Term::ReadKey::ReadMode('noecho');

    print "Type in your secret password: ";
    my $password = Term::ReadKey::ReadLine(0);

    # Rest the terminal to what it was previously doing
    Term::ReadKey::ReadMode('restore');

    # The one you typed didn't echo!
    print "\n";

    # get rid of that pesky line ending (and works on Windows)
    $password =~ s/\R\z//;

    # say "Password was <$password>"; # check what you are doing :)

    return $password;
}

关于perl - Perl 如何在不显示终端的情况下提示输入密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39801195/

相关文章:

javascript - 启用/禁用许多具有不同 ID 的单选按钮

perl - CPAN shell 内存不足。如何在 Unix 上给它更多内存?

string - 为什么这些值有时未定义?

java - 将终端添加到 Swing 应用程序中

linux - 使用 sed 命令重复字符串中的数字两次

hash - 散列密码的最佳实践 - SHA256 还是 SHA512?

perl - 解决 Net::OpenSSH 问题并将多个命令传递到路由器

java - 在 Java 中包含 Perl

linux - 如何在没有编辑器的情况下将变量 append 到 Shell 文档

php - 如何使用散列密码后登录页面