我希望自动输入密码在运行安装脚本。我已经使用调用在Perl反引号中的安装脚本。现在我的问题是,我该如何使用expect或别的东西输入密码?

my $op = `install.sh -f my_conf -p my_ip -s my_server`;

当执行了上述,密码行被打印:

Enter password for the packagekey: 

在上面的行我要输入密码。

有帮助吗?

解决方案

使用 Expect.pm

此模块是专为的,其需要用户反馈应用程序化控制定制

#!/usr/bin/perl

use strict;
use warnings;

use Expect;

my $expect      = Expect->new;
my $command     = 'install.sh';
my @parameters  = qw(-f my_conf -p my_ip -s my_server);
my $timeout     = 200;
my $password    = "W31C0m3";

$expect->raw_pty(1);  
$expect->spawn($command, @parameters)
    or die "Cannot spawn $command: $!\n";

$expect->expect($timeout,
                [   qr/Enter password for the packagekey:/i, #/
                    sub {
                        my $self = shift;
                        $self->send("$password\n");
                        exp_continue;
                    }
                ]);

其他提示

如果程序读取从标准输入密码,则可以只是管它在:

`echo password | myscript.sh (...)`

如果不是,期望值或伪终端

您可以保存在一个文件中的密码,当运行安装脚本,从文件中读取密码。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top