【发布时间】:2010-11-24 21:40:38
【问题描述】:
我希望在运行安装脚本时自动输入密码。我已经使用 Perl 中的反引号调用了安装脚本。现在我的问题是如何使用expect 或其他方式输入该密码?
my $op = `install.sh -f my_conf -p my_ip -s my_server`;
执行上述操作时,会打印密码行:
Enter password for the packagekey:
在上面一行我想输入密码。
【问题讨论】:
我希望在运行安装脚本时自动输入密码。我已经使用 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。
【讨论】: