Namaste monks.

Ive updated the question to make it more readable, Hopefully it is.

The problem is that I have been unable to get the entire output of a command sent using expect into an array. Only 1 out of 20 lines of the output of command phydrv are getting stored in @command_op.

To make it simpler, the statement that im confused about is my $result = $exp->exp_before(); in the expect_execute subroutine. Im not able to figure out why $result is not getting the entire output of the command.

I took the expect_execute subroutine from Expect command output parser sub and modified it a bit (just stripped it down, Ive included comments in the code below), but im sure ive done something wrong.

Kindly have a look at my code, and help me solve the problem.

I am new to perl and this is the first time im using expect, so please be kind!

#!/usr/bin/perl -w use Expect; #use diagnostics; $\ = "\n"; my $user = 'administrator'; my $password = 'password'; my $timeout = undef; my $exp; my $ip = '192.168.5.106'; my $result; my @command_op; sub Login { $exp = Expect->new(); $exp->debug(2); $exp->spawn ('ssh', '-l', $user, $ip); $exp->log_file("Results.log"); $exp->expect($timeout, '-re', "$user\@$ip\'s password: \$"); $exp->send ("$password\n"); } sub CountArray { $exp->expect($timeout, 're', "$user\@cli>"); @command_op = expect_execute('phydrv'); } ## This subroutine was taken from [Expect command output parser sub] sub expect_execute($) { $exp->clear_accum(); my $command = shift; my $x = ''; my $temp = ''; ##Removed 3 lines frm original because command was being passed withou +t a ; at the end. my @temp; print $exp "$command\n"; ##Just printing $command."\n", as opposed to what was done in the orig +inal subroutine. $exp->expect($timeout, 're', "$user\@cli>"); ##Didnt really understand what was being done in the original, so this + is what I thought should have been here. my $result = $exp->exp_before(); ##This line just dosen't work, Ive tried it as a direct call in the ou +ter code, but get the same results:( ( my @result ) = split( /\n/, $result ); $result = ''; foreach $x ( @result ) { $temp = $x; if ( chop( $temp ) eq "\r" ) { chop( $x ); } push (@temp, $x); } return @temp; } sub Logout { $exp->expect($timeout, 're', "$user\@cli>"); $exp->send("logout\n"); $exp->soft_close(); } Login; CountArray; Logout; #print $result; print join ("\n", @result);

This is a sample actual output,

(...the login subroutine's output is here...) administrator@cli> phydrv ====================================================================== +========= PdId Model Type CfgCapacity Location OpStatus ConfigSt +atus ====================================================================== +========= 1 WDC WD2503 SAS HDD 232.83 GB Encl1 Slot1 OK Unconfig +ured 2 WDC WD2503 SAS HDD 232.83 GB Encl1 Slot2 OK Unconfig +ured 3 WDC WD2503 SAS HDD 232.83 GB Encl1 Slot3 OK Unconfig +ured 4 WDC WD2503 SAS HDD 232.83 GB Encl1 Slot4 OK Unconfig +ured 5 WDC WD2503 SAS HDD 232.83 GB Encl1 Slot5 OK Unconfig +ured 6 WDC WD2503 SAS HDD 232.83 GB Encl1 Slot6 OK Unconfig +ured 7 WDC WD2503 SAS HDD 232.83 GB Encl1 Slot7 OK Unconfig +ured 8 WDC WD2503 SAS HDD 232.83 GB Encl1 Slot8 OK Unconfig +ured 9 WDC WD2503 SAS HDD 232.83 GB Encl1 Slot9 OK Unconfig +ured 10 WDC WD2503 SAS HDD 232.83 GB Encl1 Slot10 OK Unconfig +ured ...(some more entries actually come here, followed by 2 new lines this + one being one of them) administrator@cli> administrator@cli> logout (...the logout subroutine's output is here...)

When I print @command_op I only get,

phydrv ====================================================================== +========= PdId Model Type CfgCapacity Location OpStatus ConfigSt +atus ====================================================================== +========= 1 WDC WD2503 SAS HDD 232.83 GB Encl1 Slot1 OK Unconfig +u

In reply to Struggling to collect o/p of command using expect. by dannyd

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.