How can I use Expect to enter a password for a Perl script?

眉间皱痕 提交于 2019-12-21 16:59:40

问题


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.


回答1:


use 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;
                    }
                ]);



回答2:


If the program reads the password from standard input, you can just pipe it in:

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

If not, Expect or PTYs.




回答3:


You can save the password in a file and while running the install script, read the password from the file.



来源:https://stackoverflow.com/questions/1366751/how-can-i-use-expect-to-enter-a-password-for-a-perl-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!