如何使用Expect输入Perl脚本的密码? [英] How can I use Expect to enter a password for a Perl script?

查看:101
本文介绍了如何使用Expect输入Perl脚本的密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在运行安装脚本时自动输入密码.我已经使用Perl中的反引号调用了安装脚本.现在我的问题是如何使用expect或其他方式输入该密码?

I wish to automatically enter a password while running an install script. I have invoked the install script using the backticks in Perl. Now my issue is how do I enter that password using expect or something else?

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

执行上述操作时,将打印密码行:

When the above is executed, a password line is printed:

Enter password for the packagekey: 

在上面的行中,我想输入密码.

In the above line I wish to enter the password.

推荐答案

使用 Expect.pm .

此模块专门针对需要用户反馈的应用程序进行编程控制

This module is especially tailored for programatic control of applications which require user feedback

#!/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;
                    }
                ]);

这篇关于如何使用Expect输入Perl脚本的密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆