如何使用Expect为Perl脚本输入密码?
我希望在运行安装脚本时自动输入密码。我使用Perl中的backticks调用了安装脚本。现在我的问题是如何使用如何使用Expect为Perl脚本输入密码?,perl,expect,Perl,Expect,我希望在运行安装脚本时自动输入密码。我使用Perl中的backticks调用了安装脚本。现在我的问题是如何使用expect或其他方法输入密码 my $op = `install.sh -f my_conf -p my_ip -s my_server`; 执行上述操作时,将打印密码行: Enter password for the packagekey: 在上面的行中,我希望输入密码 使用 该模块特别适用于需要用户反馈的应用程序的编程控制 #!/usr/bin/perl use stric
expect
或其他方法输入密码
my $op = `install.sh -f my_conf -p my_ip -s my_server`;
执行上述操作时,将打印密码行:
Enter password for the packagekey:
在上面的行中,我希望输入密码 使用
该模块特别适用于需要用户反馈的应用程序的编程控制
#!/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或PTYs。您可以将密码保存在一个文件中,并在运行安装脚本时从文件中读取密码。它不起作用,因为如果在密码中使用任何特殊字符。字符串中的示例错误消息,@ug现在必须在gsx.pl第5行的“echo”附近写为\@ugRRRRR@ug“全局符号“@ug”要求在gsx.pl第5行显示包名。如果密码中有特殊字符,请在上下文中找到正确的转义方法。它存在。