Net-Telnet-Brcd
view release on metacpan or search on metacpan
lib/Net/Telnet/Brcd.pm view on Meta::CPAN
use base qw(Net::Brcd Exporter);
# Variables de gestion du package
our $VERSION = 1.13;
# Variables privées
my $_brcd_prompt = '\w+:\w+>\s+';
my $_brcd_commit = 'yes, y, no, n';
my $_brcd_continue = 'Type <CR> to continue, Q<CR> to stop:';
my $_brcd_prompt_re = "/(?:${_brcd_prompt}|${_brcd_continue}|${_brcd_commit})/";
my $_brcd_timeout = 20; # secondes
sub new {
my ($class)=shift;
my $self = $class->SUPER::new();
bless $self, $class;
return $self;
}
sub proto_connect {
my ($self, $switch, $user, $pass) = @_;
my $proto = new Net::Telnet (Timeout => ${_brcd_timeout},
Prompt => "/${_brcd_prompt}/",
);
$proto->errmode("return");
unless ($proto->open($switch)) {
croak __PACKAGE__,": Cannot open connection with '$switch': $!\n";
}
unless ($proto->login($user, $pass)) {
croak __PACKAGE__,": Cannot login as $user/*****: $!\n";
}
$self->{PROTO} = $proto;
# Retourne l'objet TELNET
return $proto;
}
sub cmd {
my ($self, $cmd, @cmd)=@_;
DEBUG && warn "DEBUG: $cmd, @cmd\n";
my $proto = $self->{PROTO} or croak __PACKAGE__, ": Error - Not connected.\n";
if (@cmd) {
$cmd .= ' "' . join('", "', @cmd) . '"';
}
$self->sendcmd($cmd);
#sleep(1); # Temps d'envoi de la commande
# Lecture en passant les continue
@cmd = ();
CMD: while (1) {
my ($str, $match) = $proto->waitfor(${_brcd_prompt_re});
DEBUG && warn "DEBUG:: !$match!$str!\n";
push @cmd, split m/[\n\r]+/, $str;
if ($match eq ${_brcd_commit}) {
$proto->print('yes');
next CMD;
}
if ($match eq ${_brcd_continue}) {
$proto->print("");
next CMD;
}
last CMD;
}
@cmd = grep {defined $_} @cmd;
$self->{OUTPUT} = \@cmd;
return @cmd;
}
sub sendcmd {
my ($self, $cmd) = @_;
my $proto = $self->{PROTO} or croak __PACKAGE__, ": Error - Not connected.\n";
DEBUG && $proto->dump_log("/tmp/telnet.log");
DEBUG && warn "Execute: $cmd\n";
unless ($proto->print($cmd)) {
croak __PACKAGE__,": Cannot send '$cmd': $!\n";
}
return 1;
}
sub sendeof {
my ($self) = @_;
my $proto = $self->{PROTO} or croak __PACKAGE__, ": Error - Not connected.\n";
unless ($proto->print("\cD")) {
croak __PACKAGE__,": Cannot Ctrl-D: $!\n";
}
return 1;
}
sub readline {
my ($self, $arg_ref) = @_;
#my ($str, $match) = $proto->waitfor(m/^\s+/);
my $proto = $self->{PROTO} or croak __PACKAGE__, ": Error - Not connected.\n";
#DEBUG && warn "DEBUG:: <$str>:<$match>\n";
#return $str;
my $str = $proto->getline(($arg_ref?%{$arg_ref}:undef));
if ($str =~ m/{_brcd_prompt_re}/) {
return;
}
return $str;
}
sub DESTROY {
my $self = shift;
$self->{PROTO}->close() if exists $self->{PROTO};
( run in 1.385 second using v1.01-cache-2.11-cpan-71847e10f99 )