ivo Welch
ivo Welch

Reputation: 2856

forcing keyboard (not stdin) input in perl despite redirected IO

perl, unix (OSX, linux): is it possible via perl to force a prompt and keyboard user input, even if the user has redirected STDIN and STDOUT? this is useful to help prevent users from storing passwords. I know it won't be perfect, but at least it will be helpful.

Upvotes: 3

Views: 293

Answers (1)

ivo Welch
ivo Welch

Reputation: 2856

thank you, everyone. here is the working version:

#!/usr/bin/perl -w
use strict;

sub term_interactive {  return -t STDIN && -t STDOUT; }
print "terminal is ".((term_interactive)?"interactive":"batch")."\n";

print "type password : ";

use Term::ReadKey;
ReadMode 2; # noecho
open(my $KEYIN, "/dev/tty") or die "cannot open tty for read\n";
print("read ".<$KEYIN>."\n");
close($KEYIN);
ReadMode 0; # restore terminal
## or ReadMode 1; # normal terminal

Upvotes: 1

Related Questions