在 telnet waitfor 中匹配表达式时如何让 perl 脚本死掉?

How to have perl script die when expression is matched in telnet waitfor?

我想知道当表达式匹配时如何让 telnet waitfor 函数进入错误模式。当表达式不匹配时,它将等待 Timeout 指定的时间并继续。例如:

$ok = my_tel->waitfor('/I have matched/', Timeout => 5)
if(ok){
   die "I have matched. Time to die /n";
}

但是,对于上面的代码,脚本将检查 telnet、超时并在执行 if 部分之前退出脚本。

我假设您正在使用 Net::Telnet?

如果您指定 return 的错误模式,那么 waitfor 将 return undef 而不是死亡。你的代码看起来像这样

my $matched = $my_tel->waitfor(
    Match   => '/I have matched/',
    Timeout => 5,
    Errmode => 'return',
);

if ( $matched ) {
    die "I have matched. Time to die/n";
}