【问题标题】:How to read password line after pipe input to Perl script如何在管道输入到 Perl 脚本后读取密码行
【发布时间】:2018-10-27 02:33:58
【问题描述】:

我正在尝试读取STDIN,然后获取用户输入行而不在终端中显示它。

Term::ReadKeyReadMode('noecho') 解决方案将不起作用,因为它使用<STDIN>,如果它不是空的,它会立即将(应该是文件,例如管道数据)作为 输入 并且实际上不起作用:

use warnings;
use strict;
use Term::ReadKey;

my $_stdin = <STDIN>;
print "Enter your password:\n";
ReadMode('noecho');
my $_pass = ReadLine(0); # This one uses <STDIN>!
ReadMode(0);
print "STDIN:\n$_stdin\nPassword:\n$_pass\n";

输出:

$ echo "some data" | perl term-readkey.pl
Enter your password:
Use of uninitialized value $_pass in concatenation (.) or string at term-readkey.pl line 10, <STDIN> line 1. 
STDIN:
some data

Password:

我提供的唯一解决方案是使用Term::ReadLine,这似乎没有使用&lt;STDIN&gt; 作为Term::ReadKey,但问题是$_term-&gt;readline() 的输出是可见的:

use warnings;
use strict;
use Term::ReadLine;

my $_stdin = <STDIN>;
my $_term = Term::ReadLine->new('term');
my $_pass = $_term->readline("Enter your password:\n");
print "STDIN:\n$_stdin\nPassword:\n$_pass\n";

输出:

$ echo "some data" | perl term-readkey.pl
Enter your password:
25 # actually entered it, and its visible...
STDIN:
some data

Password:
25

有一个similar question,但答案只适用于 Unix'y 系统并且输入可见...

【问题讨论】:

  • 请注意,ReadLine() 被记录为不能在 Windows 上工作(但您可以改为使用 &lt;STDIN&gt;)。您是否希望在特定的非 Unixy 架构上运行它?
  • 您可以尝试将Term::ReadLinefindConsolemetacpan.org/pod/Term::ReadLine#findConsole返回的设备名称与Unix'y系统的解决方案结合起来
  • @Ujin 谢谢,成功了。
  • @Ujin 但是又是可见...
  • 啊找到了,用ReadMode('noecho', TTY)...

标签: perl pipe stdin


【解决方案1】:

所以我找到了解决方案,这很简单:

Term::ReadKey 的ReadMode 与Term::ReadLine 的术语IN 一起使用,例如:

use Term::ReadLine;
use Term::ReadKey;

my $_stdin = <STDIN>;
my $_term = Term::ReadLine->new('term');
ReadMode('noecho', $_term->IN);
my $_pass = $_term->readline("Enter your password:\n");
ReadMode(0, $_term->IN);
print "STDIN:\n$_stdin\nPassword:\n$_pass\n";

或者(感谢Ujin

use Term::ReadLine;
use Term::ReadKey;

my $_stdin = <STDIN>;
my $term = Term::ReadLine->new('term');
my @_IO = $term->findConsole();
my $_IN = $_IO[0];
print "INPUT is: $_IN\n";
open TTY, '<', $_IN;
print "Enter your password:\n";
ReadMode('noecho', TTY);
my $_pass = <TTY>;
ReadMode(0, TTY);
close TTY;
print "STDIN:\n$_stdin\nPassword:\n$_pass\n";

输出:

Enter your password:
     # here enter hiddenly
STDIN:
stdin input

Password:
paws

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-29
    • 2015-10-28
    • 2022-12-16
    • 2010-11-24
    • 2012-06-01
    • 2010-10-22
    • 2013-10-24
    • 1970-01-01
    相关资源
    最近更新 更多