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 |