If you have ever used Expect to execute commands on a remote system I am sure you have run into the problem of parsing the command output from the output of the Expect methods exp_before(), clear_accum(), etc... . If you send your command to this subroutine it will parse the output for you and return the command output as it would be returned from a backtick execution.
Note: This subroutine uses a global Expect object variable which already has an established connection. The subroutine will not take commands that end in & .. for those commands just use $expect->print method.
# Executes a command via a global expect object and
# returns the output of the command.
#
# Arguments:
# 1 - Command <string variable containing the command string
+>
# Returns:
# String variable cotaining the output of the specified command.
#
sub expect_execute($) {
$expect->clear_accum();
my $command = shift;
my $x = '';
my $temp = '';
if ( $command =~ /;$/ ) {
chop( $command );
}
print $expect "$command | sed 's/^/COMMAND_OUT: /g'; echo -n E
+ND_; echo EXPECT\n";
$expect->expect( 300, -re => '^END_EXPECT' );
my $result = $expect->exp_before();
( my @result ) = split( /\n/, $result );
$result = '';
foreach $x ( @result ) {
$temp = $x;
if ( chop( $temp ) eq "\r" ) {
chop( $x );
}
if ( $x =~ m/^COMMAND_OUT: / ) {
$temp = substr( $x, 13 );
$result = $result . $temp . "\n";
}
}
return $result;
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.