Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Perl Expect not matching

by rayvn11 (Initiate)
on Jan 02, 2014 at 17:16 UTC ( [id://1068998]=perlquestion: print w/replies, xml ) Need Help??

rayvn11 has asked for the wisdom of the Perl Monks concerning the following question:

I'm admittedly a perl novice, and I am trying figure out how to use the expect.pm module. I've never used expect in any form previously. I can see that expect is trying to match my pattern, but it doesn't appear to work, not sure what I am missing. Any help would be great!

Code snippet
my $match = 'Do you want to unpack these archive files for processing? + [Y/N]: '; #my $exp = Expect->spawn($command, $params) or die "Cannot spawn \n"; my $exp = Expect->spawn("/usr/bin/python /opt/vtools/Scripts/reportcon +solidator.py /opt/vtools/Healthcheck/$dDate > /opt/vtools/HealthRepor +t.out") or die "Cannot spawn \n"; unless ($exp->expect(3,'-re',$match)) {}; print $exp "$response1\r"; #unless ($exp->expect(3,"Do you want to unpack these archive files + for processing? [Y/N]:")) {}; #print $exp "$response1\r"; #unless ($exp->expect(700,"Do you want to process these CDR files? + [Y/N]: ")) {}; #print $exp "$response2\r"; #unless ($exp->expect(5,"Do you want to process these health summa +ry files? [Y/N]: ")) {}; #print $exp "$response1\r"; $exp->soft_close();
OUTPUT Snippet
vtools@txslws1d03:/opt/vtools/Scripts$ ./Fullhealthprep.pl Spawned '/usr/bin/python /opt/vtools/Scripts/reportconsolidator.py /op +t/vtools/Healthcheck/01_02_2014 > /opt/vtools/HealthReport.out' spawn id(3) Pid: 11561 Tty: /dev/pts/0 at /usr/local/share/perl/5.14.2/Expect.pm line 181 Expect::spawn('Expect', '/usr/bin/python /opt/vtools/Scripts/r +eportconsolidator.py /op...') called at ./Fullhealthprep.pl line 27 expect(Expect=GLOB(0x9f695bc) 3 -re Do you want to unpack these archiv +e files for processing? [Y/N]: ) called... expect(): handling param '-re'... expect(): handling option '-re'... Starting EXPECT pattern matching... at /usr/local/share/perl/5.14.2/Expect.pm line 561 Expect::expect('Expect=GLOB(0x9f695bc)', 3, '-re', 'Do you wan +t to unpack these archive files for processing? [Y/...') called at ./ +Fullhealthprep.pl line 29 spawn id(3): beginning expect. Timeout: 3 seconds. Current time: Thu Jan 2 17:10:51 2014 spawn id(3): list of patterns: #1: -re `Do you want to unpack these archive files for processing? [ +Y/N]: ' spawn id(3): Does `' match: pattern #1: -re `Do you want to unpack these archive files for proce +ssing? [Y/N]: '? No. Waiting for new data (3 seconds)... TIMEOUT Returning from expect with TIMEOUT or EOF Closing spawn id(3). at /usr/local/share/perl/5.14.2/Expect.pm line 1354 Expect::soft_close('Expect=GLOB(0x9f695bc)') called at ./Fullh +ealthprep.pl line 37 Y Got EOF from spawn id(3). spawn id(3) closed. Pid 11561 of spawn id(3) exited, Status: 0x00 Closing spawn id(3). at /usr/local/share/perl/5.14.2/Expect.pm line 1431 Expect::hard_close('Expect=GLOB(0x9f695bc)') called at /usr/lo +cal/share/perl/5.14.2/Expect.pm line 1621 Expect::DESTROY('Expect=GLOB(0x9f695bc)') called at ./Fullheal +thprep.pl line 0 eval {...} called at ./Fullhealthprep.pl line 0

Replies are listed 'Best First'.
Re: Perl Expect not matching
by Paladin (Vicar) on Jan 02, 2014 at 20:09 UTC
    I don't have much experience with Expect.pm, but given what I've read above, your command is redirecting output to a file, and it seems that Expect is never seeing the prompt it's expecting because that's been redirected to the file.
    my $exp = Expect->spawn("/usr/bin/python /opt/vtools/Scripts/reportcon +solidator.py /opt/vtools/Healthcheck/$dDate > /opt/vtools/HealthRepor +t.out") or die "Cannot spawn \n";
    and
    spawn id(3): Does `' match: pattern #1: -re `Do you want to unpack these archive files for proce +ssing? [Y/N]: '? No.
      That was it! I was sending output to a file and expect wasn't seeing anything. Thanks!!
Re: Perl Expect not matching
by Crackers2 (Parson) on Jan 02, 2014 at 19:10 UTC

    The $exp->expect(3,'-re',$match) makes it treat $match as a regexp instead of a string. That causes the ? and [Y/N] to be not properly recognized.

      I've also tried doing it this way, but I get a similar issue.
      unless ($exp->expect(3,"Do you want to unpack these archive files +for processing? [Y/N]:")) {}; print $exp "$response1\r";
      Output is similar, I've tried escaping the ? and brackets as well.
      spawn id(3): list of patterns: #1: -ex `Do you want to unpack these archive files for processing? [ +Y/N]:' spawn id(3): Does `' match: pattern #1: -ex `Do you want to unpack these archive files for proce +ssing? [Y/N]:'? No. Waiting for new data (3 seconds)... TIMEOUT Returning from expect with TIMEOUT or EOF Closing spawn id(3). at /usr/local/share/perl/5.14.2/Expect.pm line 1354 Expect::soft_close('Expect=GLOB(0x96965bc)') called at ./Fullh +ealthprep.pl line 37

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1068998]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found