Skip Menu |

This queue is for tickets about the Expect CPAN distribution.

Report information
The Basics
Id: 87967
Status: rejected
Priority: 0/
Queue: Expect

People
Owner: Nobody in particular
Requestors: theo [...] ubuntucy.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Using expect with cryptsetup
Date: Tue, 20 Aug 2013 09:32:55 +0300
To: bug-Expect [...] rt.cpan.org
From: Theodotos Andreou <theo [...] ubuntucy.org>
Hi guys, I am not sure if this is an Expect.pm bug or my lack of understanding perl. Feel free to close it if it is not a real bug. I am trying to automate the decryption of a volume using Expext.pm I want to run this command automated: # cryptsetup luksOpen /dev/VG/DATA dec-DATA Enter passphrase for /dev/VG/DATA: [download] <http://perlmonks.org/index.pl?abspart=1;displaytype=displaycode;node_id=1050126;part=1> After you successfully give the passphrase you get /dev/mapper/dec-DATA which is the unencrypted volume I want to automate this using Expect.pm I came up with this code: #!/usr/bin/perl use strict; use warnings; use Expect; use Getopt::Std; $Expect::Debug = 0; $Expect::Log_Stdout = 1; my $command = "/sbin/cryptsetup"; my $timeout = 5; my $exp = Expect->spawn( $command, 'luksOpen', '/dev/VG/DATA', 'dec-DATA' ) or die "Cannot spawn $command: $!\n"; my $idx = $exp->expect($timeout, [qr/Enter\s+passphrase\s+for/, sub { my $exp =shift; $exp->send("secret\n"); }] ); $exp->soft_close(); [download] <http://perlmonks.org/index.pl?abspart=1;displaytype=displaycode;node_id=1050126;part=2> Now I have the following peculiarity. When $Expect::Debug is set to 0 it does not work; when set to 1 it does! Can you please enlight a perl newbie? Is this a bug or am I doing something fundamentally wrong? I use perl v5.14.2 and Expect.pm is version 1.21.
Am Di 20. Aug 2013, 02:33:22, theo@ubuntucy.org schrieb: Show quoted text
> I came up with this code: > > #!/usr/bin/perl use strict; use warnings; use Expect; use Getopt::Std; > $Expect::Debug = 0; $Expect::Log_Stdout = 1; my $command = > "/sbin/cryptsetup"; my $timeout = 5; my $exp = Expect->spawn( > $command, > 'luksOpen', '/dev/VG/DATA', 'dec-DATA' ) or die "Cannot spawn > $command: > $!\n"; my $idx = $exp->expect($timeout, [qr/Enter\s+passphrase\s+for/, > sub { my $exp =shift; $exp->send("secret\n"); }] ); $exp-
> >soft_close();
Looks good so far. Show quoted text
> Now I have the following peculiarity. When $Expect::Debug is set to 0 > it does not work; when set to 1 it does! Can you please enlight a perl > newbie? Is this a bug or am I doing something fundamentally wrong?
:-) Sounds like you have a race condition there. $Expect::Debug=1 slows things down, so it works then. The problem is: you are matching only the start of the (multiline?) prompt, so when the programs output is only "Enter Passphrase for" you already send the password before the program is listening for it. Either enhance the RE to match until the final character of the prompt (preferred but tricky) or add a short delay (sleep 1; quick-n-dirty and not totally reliably, but probably good enough). Hope this helps, Roland -- RGiersig@cpan.org
Subject: Re: [rt.cpan.org #87967] Using expect with cryptsetup
Date: Wed, 21 Aug 2013 09:49:58 +0300
To: bug-Expect [...] rt.cpan.org
From: Theodotos Andreou <theo [...] ubuntucy.org>
On 20/08/2013 12:04 μμ, Roland Giersig via RT wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=87967 > > > Am Di 20. Aug 2013, 02:33:22, theo@ubuntucy.org schrieb:
>> I came up with this code: >> >> #!/usr/bin/perl use strict; use warnings; use Expect; use Getopt::Std; >> $Expect::Debug = 0; $Expect::Log_Stdout = 1; my $command = >> "/sbin/cryptsetup"; my $timeout = 5; my $exp = Expect->spawn( >> $command, >> 'luksOpen', '/dev/VG/DATA', 'dec-DATA' ) or die "Cannot spawn >> $command: >> $!\n"; my $idx = $exp->expect($timeout, [qr/Enter\s+passphrase\s+for/, >> sub { my $exp =shift; $exp->send("secret\n"); }] ); $exp-
>>> soft_close();
> Looks good so far. >
>> Now I have the following peculiarity. When $Expect::Debug is set to 0 >> it does not work; when set to 1 it does! Can you please enlight a perl >> newbie? Is this a bug or am I doing something fundamentally wrong?
> :-) Sounds like you have a race condition there. $Expect::Debug=1 slows things down, so it works then. The problem is: you are matching only the start of the (multiline?) prompt, so when the programs output is only "Enter Passphrase for" you already send the password before the program is listening for it. Either enhance the RE to match until the final character of the prompt (preferred but tricky) or add a short delay (sleep 1; quick-n-dirty and not totally reliably, but probably good enough). > > Hope this helps, > > Roland > -- > RGiersig@cpan.org
A simple "sleep 1" after the send command resolved my issue. Thanks Roland!