Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Masking question (sort of)

Status
Not open for further replies.
Joined
May 3, 2003
Messages
90
Location
CA
Hi All,

I know this question has been asked before but I cant seem to find it. Here is the question :

How to hide input from user ?

Script asks for ftp login but i want the password (input from user) not to display while they are typing it.

Can someone help me out ??

Thanks,
 
Well I've done it one of two ways:
Code:
system("stty -echo");
$passwd=<>;
system(&quot;stty echo&quot;);
Or if you want a &quot;pure&quot; perl way:
Code:
use IO::Stty;
$o_mode=stty(\*STDIN,'-g');
stty(\*STDIN,'-echo');
$passwd=<>;
stty(\*STDIN,$o_mode);
I've only tested it on *nix systems and I doubt it'd work in winderz. Anyway I hope that helps.
 
Well,

Im on a Win32 platform and both of your examples didnt work.
still digging . . .

Any other thoughts ?
 
Just in case someone was wondering how to do it or to avoid the time and frustration, here is what I have come up with.

#!Perl -U

use strict;
use warnings;
use Win32::Console;

my $enter = chr 13;
my $backspace = chr 8;
my $space = chr 32;
my $counter = 0;

print &quot;\nInput: &quot;;

my @p = &getpass;

my @final;

foreach (@p) {

$final[++$#final] = $_ if (defined($_));

}

print @final;

sleep;


sub getpass {

my @pass;

my $in = new Win32::Console(STD_INPUT_HANDLE);
$in -> Mode(ENABLE_PROCESSED_INPUT);
$in -> Title(&quot;Enter Password&quot;);

while (my $data = $in -> InputChar()) {

if ($data eq $enter) {

print &quot;\n&quot;;
last;

}

else {

$pass[++$#pass] = $data;

if ($data eq $backspace) {

print $backspace.$space.$backspace if ($counter > 0);
undef $pass[$counter-1];
$counter--;
if ($counter < 0) { $counter = 0; }

}

else {

print &quot;*&quot;;
$counter++;

}

}

}

return @pass;

}

If anyone has a better way or different way of doing this please reply to this post.

Thanks,
 
#!perl
#
# m3h! i kn0w u! how about this? ;)
#
# # #

use strict;
use Term::ReadKey;

my $s = &quot;&quot;;

print &quot;type! then hit enter\n&quot;;

ReadMode 2;

$s = <stdin>;

ReadMode 0;

chomp($s);

print &quot;u safely typed this : &quot; . $s;

---------------------------------------
m3h
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top