$exp
->
send
(
$string
);
$exp
->expect(
$timeout
,
[
qr/expect_something/
i,
sub
{ do_something);exp_continue; }],
);
$s
=
$exp
->
before
();
# $s就得到 匹配expect_somthing之前的字符串,如果匹配失败,得到所有的输出。
所以在下面的示例中,必须加这一句$exp->expect(1, undef);,才能获得vsx命令的输出结果
my $exp = Expect->new;
my $cmd = "ssh $FIR_USER\@$FIREWALL";$exp->spawn($cmd) or die "cannot spawn $cmd:$!\n";
$exp->expect(3,
[qr/\(yes\/no\)\?/i, sub{
$exp->send("yes\n");
exp_continue;
}],
[qr/$FIREWALL's\s*password:/i, sub{
$exp->send("$FIR_PSW\r");
}]
);
$exp->expect(3,
[qr/admin@\w+>/i, sub{
$exp->send("vxs\n");
}]
);
$exp->expect(1, undef); //必须加这一句,因为before得到的是上一个匹配之前的字符串,这里得到的是undef之前的输出,即vsx的输出
my $out = $exp->before();
$exp->soft_close();
if($out =~ m/enter\s*the\s*vxWorks\s*shell/is){
$is_product = 0;
}
print "out:$out\n";
print "is_product:$is_product\n";