Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Expecting more from expect

by bret.foreman (Acolyte)
on Jul 07, 2004 at 23:51 UTC ( [id://372618]=perlquestion: print w/replies, xml ) Need Help??

bret.foreman has asked for the wisdom of the Perl Monks concerning the following question:

Thanks, that worked like a charm. As for using scp, I quote from CPAN(

"It's probably better just to use SSH1 or OpenSSH <>"

And also, the scp module is not included in my PERL re-distribution license. I could install it for development, but I couldn't ship it. And making the end-users download and install it is more work than you might imagine.


Replies are listed 'Best First'.
Re: Expecting more from expect
by TilRMan (Friar) on Jul 08, 2004 at 01:36 UTC

    You may need to reset the accumulator size. From the Expect manpage:

    $object->max_accum($maximum_buffer_length | undef)

    Set the maximum accumulator size for object. This is useful if you think that the accumulator will grow out of hand during expect() calls. Since the buffer will be matched by every match_pattern it may get slow if the buffer gets too large. Returns current value if called without parameters. Not defined by default.

    P.S. The scp(1) program might be a better way to go.

      Hmmm. Maybe this is a bug in my version of PERL (v5.6.1 on Linux). I made the following test program called "":
      #!/usr/bin/perl use strict; use Expect; my $object = Expect->spawn( "bash" ) or die "Could not spawn bash shell.\n"; $object->log_stdout(0); $object->max_accum( 128 * 512 ); my $header = "Test Log Header"; $object->send( "cat test_log.txt\n" ); my ($matched_pattern_position, $error, $successfully_matching_string, $before_match, $after_match) =$object->expect( 5 , -re => $header ); if( $error ne undef ) { die "Could not find header.\n"; } my @rows = split /^/ , $after_match; printf "Found %d lines after match in file.\n", scalar( @rows );
      And created the file "test_log.txt" that looks something like this:
      Test Log Header 040702003113 21% 1175 1859 8239 8744 2221 0 0 4 100% 7% T 6% 040702003113 21% 1175 1859 8239 8744 2221 0 0 4 100% 7% T 6% 040702003113 21% 1175 1859 8239 8744 2221 0 0 4 100% 7% T 6% 040702003113 21% 1175 1859 8239 8744 2221 0 0 4 100% 7% T 6% 040702003113 21% 1175 1859 8239 8744 2221 0 0 4 100% 7% T 6% ...for 160 lines...
      Then see the following:
      Output of "wc test_log.txt": 161 2243 9776 test_log.txt Ouput of "": Found 34 lines after match in file.
      So there's no ssh or any other complication, just the basic expect buffer of a cat result. Can someone else try this and confirm that it works as expected on their system?



        My apologies -- I read the OP too hastily.

        Again, will scp do the trick?

        You are trying to use the $after_match to grab everything after a match. This is not the way Expect works. It will only read what it needs in order to match the pattern you specified. You need to match the end of the file and then take everything before the end.

        #untested my $prompt = "my_unique_custom_prompt_aint_it_neat>"; $object->send("export PS1=$prompt\r"); $object->expect(10, $prompt); $object->send("cat file.txt\r"); $object->expect(10, $header); $object->expect(120, $prompt); my $contents = $object->before();

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-21 09:18 GMT
Find Nodes?
    Voting Booth?

    No recent polls found