tags:

views:

44

answers:

1

Hi I am using Expect in perl to connect to remote machine and execute certain functions. sample code is like

$outfile="ls -lrt";
$outfile1="output";

$exp->expect(30,-re,".*bash-.*" => sub{$exp->send("$outfile2 >$outfile \r")});
$exp->expect(60,-re,".*bash-.*" => sub{$exp->send("$shayam > $Ram \r")});

Even if the first expression fails it will wait for 60 sec and will execute the second statement. I just want to make a check that if only the first statement passes it should proceed.

+3  A: 

I am guessing you are using the Expect.pm module documented here. As stated there:

If called in an array context expect() will return ($matched_pattern_position, $error, $successfully_matching_string, $before_match, and $after_match).

So you likely want to call it in array context so you can get an error, both if the regex fails and if the send fails.

my ($matched_pattern_position, $error,
  $successfully_matching_string,
  $before_match, $after_match) =
  $exp->expect(30
  , -re,".*bash-.*" =>
    sub{$exp->send("$outfile2 >$outfile \r")}
);

$exp->expect(60
  ,-re,".*bash-.*" =>
    sub{$exp->send("$shayam > $Ram \r")}
) if !defined $error;
Conspicuous Compiler