use warnings;
use strict;
+use Moose;
+
use Net::Telnet;
use Data::Dump qw(dump);
+use Moose::Util::TypeConstraints;
+use Regexp::Common qw(net);
+
+subtype IPAddr
+=> as Str
+=> where {/^$RE{net}{IPv4}$/}
+=> message { 'invalid IP address'};
+
+subtype Port
+=> as Str
+=> where { m{\d+(/\d+)+} }
+=> message { 'invalid port' };
+
+has 'ip' => ( is => 'rw', isa => 'IPAddr' );
+has 'port' => ( is => 'rw', isa => 'Port' );
our $telnet;
sub telnet {
- my ($ip) = @_;
+ my $self = shift;
+
+ my $ip = $self->ip;
return $telnet->{$ip} if exists $telnet->{$ip};
$t->dump_log('/tmp/log') if $ENV{DEBUG};
- warn "open";
+ warn "open $ip";
$t->open( $ip );
$t->print("");
return $telnet->{$ip} = $t;
}
-sub hash {
- my ($class,$ip,$port) = @_;
-
- warn "# hash $ip $port";
-
- my $t = telnet($ip);
-
sub command {
- my $command = shift;
+ my ($self,$command) = @_;
- $command .= ' ' . $port;
+ $command .= ' ' . $self->port;
+
+ my $t = $self->telnet;
warn "# $command\n";
$t->print($command);
}
- warn "## $ip $command ",dump $hash;
+ warn "## ", $self->ip, " $command ",dump $hash;
return $hash;
}
+sub hash {
+ my ($self) = @_;
+
+ my $ip = $self->ip;
+ my $port = $self->port;
+
+ warn "# hash $ip $port";
+
our ( $row, $hash );
sub copy {
}
}
-$hash = command 'show interface';
+$hash = $self->command('show interface');
copy qw(
AdminStatus
LinkStatus
LastLinkUpTime
);
-$hash = command 'show adsl status';
+$hash = $self->command('show adsl status');
copy qw(
LineConfProfile
);
-$hash = command 'show adsl physical';
+$hash = $self->command('show adsl physical');
copy qw(
AtucCurrSnrMgn
AtucCurrAtn
sub logout {
- my ($ip) = @_;
+ my $self = shift;
+ my $ip = $self->ip;
my $t = $telnet->{$ip} || die "no $ip telnet in ",dump($telnet);
warn "logout $ip";
}
sub DESTROY {
- warn "# telnet = ",dump($telnet);
- logout($_) foreach keys %$telnet;
+ my $self = shift;
+ warn "# DESTROY telnet = ",dump( keys %$telnet );
+ $self->logout($_) foreach keys %$telnet;
}
1;