Perl中waitfor()的可靠替代方案

时间:2014-10-23 02:27:15

标签: perl

以下是我的脚本片段,它将等待字符串"用户名:"并打印用户名进行登录,在"用户名:"之前打印大量的启动消息。看来,有时脚本丢失,找不到它并返回说"找不到用户名:",特别是当一些溢出的启动消息在"用户名:"之后打印出来。提示行,那么有更可靠的方法吗?感谢。

my %words = (
   username => 'user1',
   username_string => 'username: $',
);
( $prematch, $match ) = $obj->{comm}->waitfor(Match => "/$words{username_string}/",Errmode => "return");
if ( !defined $match ) {
    print "Failed to find username:";
    return;
}
else {
  $obj->{comm}->print( $words{username} );
}

$ obj是Net :: Telnet的对象

1 个答案:

答案 0 :(得分:0)

实际上我不知道哪种对象与你的$ obj有关。假设它可能是Net :: Telnet也许这可以帮助你:

perl-telnet-does-not-wait-for-the-end-of-the-previous-command

Futhermore:

( $prematch, $match ) = $obj->{comm}->waitfor(Match => "/$words{username_string}/",Errmode => "return");

也许这是将cmd输出捕获到数组变量

的更好方法
my @lines = $telnet->cmd($comm);
for (my $line: @lines) {
    # your matching goes here..
}

或试试这个:

$t = new Net::Telnet(Host =>"192.168.0.1", Timeout => 5,Port => "1701", Prompt => '/>\$ $/');
$t->print($comm);
$t->waitfor('/username: $/i');
print "result=", $s;