So I am having a problem getting my perl expect script to work. It works fine on most commands but there is some weirdness on others. I essentially execute this command and it appears that there are several responses before it hits the match. Each time it waits, it clears the buffer before getting the next response. Here is the debug output (edited for brevity - notice the snips):

Starting EXPECT pattern matching... at /usr/local/share/perl5/Expect.pm line 597 Expect::expect('Expect=GLOB(0x22cd510)', 60, '-re', ']MyPro +mpt>|]MyPrompt#|]MyPrompt...') called at ./get_ippools_exp.pl line 19 +2 main::cmd('Expect=GLOB(0x22cd510)', 'show config context da +ta | grep "ip pool"') called at ./get_ippools_exp.pl line 75 main::getPools() called at ./get_ippools_exp.pl line 25 spawn id(4): beginning expect. Timeout: 60 seconds. Current time: Fri Aug 26 13:50:58 2016 Waiting for new data (60 seconds)... spawn id(4): new data. spawn id(4): read 92 byte(s). show config context data | grep "ip pool" Friday August 26 18:50:58 UTC 2016 Waiting for new data (60 seconds)... spawn id(4): new data. spawn id(4): read 1815 byte(s). ip pool data1 ... ip pool data2 ... <snip> ip pool data16 ... Waiting for new data (56 seconds)... spawn id(4): new data. spawn id(4): read 2048 byte(s). ip pool data17 ... ip pool data18 ... <snip> ip pool data38 ... ip Waiting for new data (56 seconds)... spawn id(4): new data. spawn id(4): read 351 byte(s). pool data39 ... ip pool data40 ... ip pool data41 ... Waiting for new data (56 seconds)... spawn id(4): new data. spawn id(4): read 33 byte(s). <----here it receives the prompt!!!! [local]MyPrompt> Returning from expect successfully. spawn id(4): accumulator: ` '<----You can see that the buffer is dum +ped

The code to call the command is simple:

my ($exp, $cmd) = @_; $exp->print($cmd, "\n"); $exp->expect($timeout, -re => $prompt) or return; my $output = $exp->before; $output =~ s/^$cmd\n//; # remove the sent cmd return $output;
It seems like each time the spawn id(4) process goes into "Waiting for new data (.. seconds)..." mode it dumps the prior values. Can anyone help me with this? Thank you!

In reply to Perl Expect.pm by MojaveJoe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.