get the result from commands executed in expect

http://www.perlmonks.org/?node_id=58634

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;
}

Advertisements

2 thoughts on “get the result from commands executed in expect

  1. Thank you!!!! I’ve been trying to figure out how to determine completion of a command, your approach is clever. If we can’t get a clear indication by command, we can just insert our own! Sweet!

  2. This is brilliant!!!! Thank you so much for this.. I’ve been fooling around with this issue for over a year and never found anything satisfactory.

    Here’s what I was using so far… it does the job..but there was a need to match for a pattern anyway…

    $sshMTT1->clear_accum(); $sshMTT1->expect(10,[qr/\$[\s]*/=> sub {$sshMTT1->send(“grep <<<<<*>>>>”);}]); @Output = $sshMTT1->expect(5); $sshMTT1->send(“\cC”); $exactOutput = $Output[3]; $exactOutput =~ tr/\cM//d;
    [download]

    The variable $exactOutput had more or less the value I was looking for and I used to pattern match on this variable… Primitive.. compared to the solution here!!!

    [reply]
    [d/l]
    [select]

    Re^2: Expect command output parser sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s