notes blog about

Get Password From User

Sometimes you need to use a password in your command line script. One of the ways how to get it, is to ask the user.

System command

Use the system command for terminal line settings - stty:

sub get_passwd {
    my $user = shift;

    system "stty -echo";               # turn off input echoing
    print "Enter password for '$user'> ";
    chomp( my $pass = <STDIN> );

    return $pass;

You can use this function like this:

my $pass = get_passwd($user);

Perl module

Or you can use the CPAN module Term::ReadKey:

use Term::ReadKey;    # simple terminal control

print "Enter ${users}'s password: ";
ReadMode 'noecho';
my $password = ReadLine 0;    # normal read using scalar(<FileHandle>)
chomp $password;
ReadMode 'normal';