Question

I have the following script that runs a command and puts the data in a DB. I need to account for the possibility of being asked for a password "password:" some of the time. How do I wrap an expect call into this?

#!/usr/software/bin/perl
use strict;
use DatabaseLib;
use Data::Dumper;
use Expect;

 #Connect to database
 my $dbh = DBI->connect($DB_CONNECT_STRING, $DB_USER, $DB_PASSWORD, { RaiseError => 1,      AutoCommit => 1 })
or die "failed to connect to database: $DB_CONNECT_STRING";

my $expect = Expect->new; 
my %burtHash;
my @cols = qw/date_create sub_by impact date-lastmod lastmod-by bug_rel case_score state s p type subtype subteam found_by target_release/;
my @burtInfo = `mycommand`;
my $timeout = 20;
my $password  = "password";

while(my $ele = shift(@burtInfo)){
my ($index, @data) = split(/\s+/, $ele);

for my $i(0 .. $#cols){
    $burtHash{$index}->{$cols[$i]} = shift(@data);
}
for my $id (keys %burtHash){
    my %burt_details;
    for my $col (keys %{$burtHash{$id}} ) {
       $burt_details{$col} = $burtHash{$id}->{$col};
    }
    if ( $id =~ /\d+/) {
        burt_update(
                $dbh,
                $id ,
                \%burt_details,
        );
    }
}
}

I think I just need to put in something like this and call it, but i'm not sure where/how:

$expect->expect($timeout,
            [   qr/password:/i, #/
                sub {
                    my $self = shift;
                    $self->send("$password\n");
                    exp_continue;
                }
            ]);
Was it helpful?

Solution

You're not using $expect anywhere there. You have to run your command via $expect->spawn so that your Expect object can handle things. And then you'll need some way of gathering its output (I'm thinking using $expect->log_file(...) to set the log to a string filehandle or something).

Once you're using $expect->spawn, then you can insert your password check. But there's no way you can do this with qx (the backticks).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top