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

Monks,

I have a version of Expect 1.15 that I am attempting to use to capture input from commands such as ls -l

However, the input appears to be get chopped as I cannot see all of the files from the listing command. I have been logging the output and also checking the accumulator.

Upon inspection of the Expect module, it appears that it performs a sysread using a fixed number of bytes.

Are there any solutions to capture the full output of a command that might dump quite a bit of output to STDOUT?


Best Regards
Ty

Update:

I used the perl debugger to walk through Expect and then I came up with a solution that works

# given an Expect object in $e # given a command to run in $command # given a timeout value in $timeout # given that command does not print ENDOFCOMMAND to STDOUT $e->send("$command;echo ENDOFCOMMAND\n"); $e->expect($timeout,[qr/(?<!echo )ENDOFCOMMAND/ => sub { 0;}], [qr/(echo ENDOFCOMMAND)/ => sub { exp_continue;}]); $outputfromcommand = $e->before();

Replies are listed 'Best First'.
Re: Expect Question
by elTriberium (Friar) on Jul 14, 2009 at 00:55 UTC

    Do you mean that you want to capture the output of ls -l?

    Why would you need Expect for this at all? Have a look at the Expect.pm documentation and the answer to the question "I just want to read the output of a process without expect()ing anything. How can I do this?": http://search.cpan.org/~rgiersig/Expect-1.15/Expect.pod

    If the output of the command is very long then I'd recommend to redirect the STDOUT of the command to a file and then later on process this file.

      I need to log into a remote machine via an intermediate proxy, so I cannot simply just read the output like in the example doc.

      I have attempted to read the output, but it only reads a small portion of the output and not the whole amount.

        OK, I now understand why you want to use Expect. I'm using it in a similar way, but I never got to the problem that it's not reading the whole output. But you can still redirect the output to a file and then process this file (you might want to copy the file to your local machine first).
Re: Expect Question
by Khen1950fx (Canon) on Jul 14, 2009 at 22:36 UTC
    I had a few problems with Expect, so I switched over to Net::SSH::Expect. Maybe you'll have better luck with this:

    #!/usr/bin/perl use strict; use warnings; use Net::SSH::Expect; use constant CMD => 'ls -l'; $Expect::Debug => 3; $Expect::Do_Soft_Close => 1; my $ssh = Net::SSH::Expect->new ( host => "somehost", user => 'someuser', password => 'password', log_stdout => 1, raw_pty => 1, timeout => 5 ); eval { my $login_output = $ssh->login(); if ($login_output !~ /Welcome/) { die "Login has failed. Login output was $login_output"; } }; my $spawn = new Expect; $spawn->log_file("log.txt", "w"); $spawn->cmd(CMD); $spawn->soft_close; exit(0);
    Update: Fixed