我希望在运行安装脚本时自动输入密码。我已经使用 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 (...)`
如果没有,Expect 或 PTY。
您可以将密码保存在文件中,并在运行安装脚本时从文件中读取密码。