如何检查Net :: Telnet会话仍在Perl中?

时间:2017-02-17 05:05:49

标签: perl telnet

我有一个Perl脚本,可以通过Net :: Telnet在远程设备上运行一些命令。有时,telnet会话将被远程端断开,因为登录超时或其他原因。我想知道如何在发送下一个命令之前检查telnet会话是否仍然有效?非常感谢!

my $tc = new Net::Telnet{Host=>$host,Port=>23};
sub checkTelnetLive { ??? }
my @commands=($cmd1,$cmd2,...);
foreach $cmd(@commands) {
    if checkTelnetLive {
        $tc->put($cmd);
        $tc->waitfor('/COMPLD/');
    }
    else {
        die "Remote equipment has disconnected session."
    }

}

1 个答案:

答案 0 :(得分:0)

my $tc = new Net::Telnet (Host=> $host, Port=>23, Errmode => 'return' );

或者您可以使用eval

eval {
      my $tc = new Net::Telnet (Host=> $host, Port=>23, Errmode => 'return' );

#. plus other code
};
warn $@ if $@;