[ale] Q ... Perl 'Expect' ...

Mills John M-NPHW64 Jmills at motorola.com
Mon Oct 4 15:23:16 EDT 2010


Chris -

Thanks for the example. I had gotten 'round to this. Ugly but works:
_________________________________________________________________
use Expect;
use strict;

$Expect::Log_Stdout = 0;

my $exp = Expect->spawn('ssh root at myserver.mydomain');
$exp->expect(5, "root # ") or die "SSH connection failed";
$exp->send("ls /\r");
$exp->expect(10, "/root # ") or die "no prompt received";
my $reply = $exp->before();
my $cmd_echo = "";
($cmd_echo, $reply) = split('\n', $reply, 2);
print "Sent: $cmd_echo\nReceived:\n$reply";
$exp->close();
_________________________________________________________________

I'll try your version too.

[Obviously] I'm a Perl Newb and I appreciate the help.

 - Mills

-----Original Message-----
From: ale-bounces at ale.org on behalf of Chris Fowler

> Now the Question:
> Typically my scripts do a simple sequence of (1) execute process on remote, and (2) pick through the response looking for specific text.
> 
> No problem doing (1) with 'Expect', but how do I capture each reply to analyze it? I looked over the Expect 'FAQ' and saw references to a session log but I don't see how to make use of it.

{
  my $buffer = ""
  sub append_buffer {
    $buffer .= $_;
  }

  sub get_buffer { return $buffer; }
  sub flush_buffer { $buffer = ""; }
}

$exp->log_file(\&append_buffer);
$exp->send("command\r");
$exp->exepect(30, ']$ ');
my $output = get_buffer();
flush_buffer()

Just an example.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/ms-tnef
Size: 3435 bytes
Desc: not available
Url : http://mail.ale.org/pipermail/ale/attachments/20101004/2bd21d83/attachment.bin 


More information about the Ale mailing list