Search code examples
regexperltelnet

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


I am wondering how to have the telnet waitfor function go into errmode when the expression is matched. When the expression does not match, it will wait the Timeout specified time and continue. For example:

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

With the code above, however, the script will check in the telnet, timeout, and exit the script before the if portion is ever executed.


Solution

  • I assume you are using Net::Telnet?

    If you specify an error mode of return then waitfor will return undef instead of dying. Your code would look like this

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