簡體   English   中英

如何使用Expect輸入Perl腳本的密碼?

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

我希望在運行安裝腳本時自動輸入密碼。 我使用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或PTYs。

您可以將密碼保存在文件中,並在運行安裝腳本時從文件中讀取密碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM