Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Struggling to collect o/p of command using expect.

by dannyd (Sexton)
on Feb 01, 2011 at 14:18 UTC ( [id://885497]=perlquestion: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re: Struggling to collect o/p of command using expect.
by toolic (Bishop) on Feb 01, 2011 at 17:36 UTC
Re: Struggling to collect o/p of command using expect.
by jethro (Monsignor) on Feb 04, 2011 at 09:53 UTC

    In the original script you see that in the line corresponding to $exp->expect($timeout, 're', "$user\@cli>"); the 're' is actually '-re'. It is also '-re' in the documentation. '-re' is a parameter to tell Expect to use the following exit patterns as regexes instead of just as search strings. 're' is interpreted just as another exit pattern and since there is a 're' in 'Unconfigu<re>d', Expect stops there

    Just remove the 're' and it should work

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 01:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found