【问题标题】:Evaluating the Expect in perl在 perl 中评估 Expect
【发布时间】:2010-07-16 20:47:17
【问题描述】:

我在 perl 中使用 Expect 连接到远程机器并执行某些功能。示例代码就像

$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")});

即使第一个表达式失败,它也会等待 60 秒并执行第二个语句。我只是想检查一下,如果只有第一个语句通过,它应该继续。

【问题讨论】:

    标签: perl expect


    【解决方案1】:

    我猜你正在使用the Expect.pm module documented here。如那里所述:

    如果在数组上下文中调用 expect() 将返回 ($matched_pa​​ttern_position, $error, $successfully_matching_string, $before_match 和 $after_match)。

    因此,您可能希望在数组上下文中调用它,以便在正则表达式失败和发送失败时都会收到错误。

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      • 2011-03-29
      • 2013-01-27
      相关资源
      最近更新 更多