open telnet session to each IP just once
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 23 May 2011 18:44:43 +0000 (20:44 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 23 May 2011 18:44:43 +0000 (20:44 +0200)
lib/H1/ZTEDSLAM.pm

index 11d4fb5..3a3c788 100755 (executable)
@@ -5,30 +5,43 @@ use strict;
 use Net::Telnet;
 use Data::Dump qw(dump);
 
-sub hash {
-       my ($self,$ip,$port) = @_;
 
-       warn "# hash $ip $port";
+our $telnet;
+
+sub telnet {
+       my ($ip) = @_;
+
+       return $telnet->{$ip} if exists $telnet->{$ip};
+
+       my $t = Net::Telnet->new( Timeout => 10, Prompt => '/#/' );
 
-my $t = Net::Telnet->new( Timeout => 10, Prompt => '/#/' );
+       $t->dump_log('/tmp/log') if $ENV{DEBUG};
 
-$t->dump_log('/tmp/log') if $ENV{DEBUG};
+       warn "open";
+       $t->open( $ip );
 
-warn "open";
-$t->open( $ip );
+       $t->print("");
+       $t->waitfor('/Login:/');
+       $t->print('admin');
+       $t->waitfor('/Password:/');
+       $t->print('admin');
+       $t->waitfor('/>/');
+       $t->print('en');
+       $t->waitfor('/Please input password:/');
+       $t->print('admin');
+       $t->waitfor('/#/');
+
+       warn "login OK";
+
+       return $telnet->{$ip} = $t;
+}
+
+sub hash {
+       my ($class,$ip,$port) = @_;
 
-$t->print("");
-$t->waitfor('/Login:/');
-$t->print('admin');
-$t->waitfor('/Password:/');
-$t->print('admin');
-$t->waitfor('/>/');
-$t->print('en');
-$t->waitfor('/Please input password:/');
-$t->print('admin');
-$t->waitfor('/#/');
+       warn "# hash $ip $port";
 
-warn "login OK";
+       my $t = telnet($ip);
 
 sub command {
        my $command = shift;
@@ -104,13 +117,26 @@ AturDMTState
 
 warn "# row = ",dump $row;
 
-warn "logout";
-$t->print('logout');
-$t->waitfor('/:/');
-$t->print('y');
-
 return $row;
 
 } # sub
+
+
+sub logout {
+       my ($ip) = @_;
+       my $t = $telnet->{$ip} || die "no $ip telnet in ",dump($telnet);
+
+       warn "logout $ip";
+       $t->print('logout');
+       $t->waitfor('/:/');
+       $t->print('y');
+
+}
+
+sub DESTROY {
+       warn "# telnet = ",dump($telnet);
+       logout($_) foreach keys %$telnet;
+}
+
 1;