1

同じ telnet 接続を介して複数のコマンドを読み取っている場合$_->{thand}->cmd($cmd)、切断されています。

これは、 への複数回の呼び出しとして現れますae_connect()。でデータを適切に送信および取得する方法はAnyEvent?

use strict;
use warnings;
use Data::Dumper;

use AnyEvent::Socket;
use AnyEvent;
use Net::Telnet;

$| = 1;

my $arr = [
  { username => "user+ct", passwd => "1234", Host => "92.242.254.8", Port => 1094 },
  { username => "user+ct", passwd => "1234", Host => "92.242.254.8", Port => 1095 },
  { username => "user+ct", passwd => "1234", Host => "92.242.254.8", Port => 1096 },
];

sub main_loop {

   my $cmd = "/ip firewall filter export";

   my $i=0;
   for (@$arr) {

     if (!$_->{thand}) {
       ae_connect($_);
       print("skip ", Dumper $_);
       next;
     }

     # print Dumper $_;
     $i++;
     my $s;
     $s = join "", $_->{thand}->cmd($cmd);
#     print "\n==1>$i  \n$s";
     $s = join "", $_->{thand}->cmd($cmd);
     $s = join "", $_->{thand}->cmd($cmd);

   }
   print "\n\n";
   #die @$arr*1 if $i;
}


sub ae_connect {
  my ($tc) = @_;

  print "=========== $tc->{Host} ============\n";
  tcp_connect $tc->{Host}, $tc->{Port} //23, sub {
    my ($fh) = @_ or return; # die "failed: $!";

    #
    my $t = new Net::Telnet->new(Fhopen => $fh) or return;

    eval { $t->login($tc->{username}, $tc->{passwd}) } or return;
    $t->timeout($tc->{Timeout});

    $tc->{thand} = $t;
    # $tc->{fh} = $fh;
  };

}

my $w = AnyEvent->timer(after => 0, interval => 1, cb => \&main_loop);

my $cv = AnyEvent->condvar;
$cv->recv;
4

1 に答える 1