From: Matt Z. <mzagrabe@d.umn.edu> - 2007-11-14 22:15:21
|
On Wed, 2007-11-14 at 15:39 -0600, Noah wrote: [...] > is there a way to capture the output with perl-expect into an array? Here are some snippets of what I have done in the past, though I do things a little differently now, you will get the idea... hopefully. ;) Subroutine to send a command to Cisco devices and capture the output in the scalar reference $receive_buffer. sub send_and_receive { my $configs =3D shift; my $send_string =3D shift; my $receive_buffer =3D shift; my $command =3D shift; my $wait_for_found =3D (@_) ? shift : undef; my $more_output =3D (@_) ? shift : '--More--'; my $continue_command =3D (@_) ? shift : " ";=20 my $wait_for =3D (@_) ? shift : '>$'; =20 # send whatever it is we are to send $command->put($send_string); # now wait for the "wait_for" meanwhile saving # all the output into receive_buffer my $done =3D undef; until ($done) { my ($prematch, $match) =3D (undef, undef); ($prematch, $match) =3D $command->waitfor(Match =3D> '/'.$more_output.'/i', Match =3D> '/'.$wait_for.'/i', Match =3D> '/\\[X\\] Exit.*:/ism', Timeout =3D> $configs->{management_timeout}); if ($receive_buffer ne undef) { $$receive_buffer .=3D $prematch.$match; } =20 if ($match =3D~ /$more_output/i) { $command->put($continue_command); } elsif ($match =3D~ /$wait_for/) { $$wait_for_found =3D 1 if ($wait_for_found); $done =3D 1;=20 } elsif ($match =3D~ /\[X\] Exit.*:/ism) { $$wait_for_found =3D 1 if ($wait_for_found); $done =3D 1;=20 } =20 } } $text_io =3D ''; &send_and_receive($configs, "show interface status\n", \$text_io, $connection, \$found_prompt); my @lines =3D split /\n/, $text_io; for (@lines) { $_ =3D &trim($_); } sub trim { my $s =3D shift; $s =3D~ s/^\s*//; $s =3D~ s/\s*$//; return $s; } =20 --=20 Matt Zagrabelny - mzagrabe@d.umn.edu - (218) 726 8844 University of Minnesota Duluth Information Technology Systems & Services PGP key 1024D/84E22DA2 2005-11-07 Fingerprint: 78F9 18B3 EF58 56F5 FC85 C5CA 53E7 887F 84E2 2DA2 He is not a fool who gives up what he cannot keep to gain what he cannot lose. -Jim Elliot |