| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # client::telnet Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Client::Telnet; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | prompt => [ qw(string_list) ], | ||
| 20 | timeout => [ qw(seconds) ], | ||
| 21 | _client => [ qw(INTERNAL) ], | ||
| 22 | }, | ||
| 23 | attributes_default => { | ||
| 24 | prompt => [ ':', '>', '$', '#', '%' ], | ||
| 25 | timeout => 5, | ||
| 26 | }, | ||
| 27 | commands => { | ||
| 28 | connect => [ qw(host port username password) ], | ||
| 29 | read_next => [ ], | ||
| 30 | }, | ||
| 31 | require_modules => { | ||
| 32 | 'Metabrik::String::Regex' => [ ], | ||
| 33 | 'Net::Telnet' => [ ], | ||
| 34 | }, | ||
| 35 | }; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub connect { | ||
| 39 | my $self = shift; | ||
| 40 | my ($host, $port, $username, $password) = @_; | ||
| 41 | |||
| 42 | $self->brik_help_run_undef_arg('connect', $host) or return; | ||
| 43 | $self->brik_help_run_undef_arg('connect', $port) or return; | ||
| 44 | $self->brik_help_run_undef_arg('connect', $username) or return; | ||
| 45 | $self->brik_help_run_undef_arg('connect', $password) or return; | ||
| 46 | |||
| 47 | my $timeout = $self->timeout; | ||
| 48 | my $prompt = $self->prompt; | ||
| 49 | |||
| 50 | my $sr = Metabrik::String::Regex->new_from_brik_init($self) or return; | ||
| 51 | |||
| 52 | my $re = $sr->encode($prompt) or return; | ||
| 53 | |||
| 54 | my $t = Net::Telnet->new( | ||
| 55 | Timeout => $timeout, | ||
| 56 | Prompt => "/$re/", | ||
| 57 | Port => $port, | ||
| 58 | ); | ||
| 59 | |||
| 60 | eval { | ||
| 61 | $t->open($host); | ||
| 62 | }; | ||
| 63 | if ($@) { | ||
| 64 | chomp($@); | ||
| 65 | return $self->log->error("connect: failed to host [$host] port [$port] with: [$@]"); | ||
| 66 | } | ||
| 67 | |||
| 68 | eval { | ||
| 69 | $t->login($username, $password); | ||
| 70 | }; | ||
| 71 | if ($@) { | ||
| 72 | chomp($@); | ||
| 73 | return $self->log->error("connect: login failed with username [$username]"); | ||
| 74 | } | ||
| 75 | |||
| 76 | $self->_client($t); | ||
| 77 | |||
| 78 | $self->log->verbose("connect: connection successful"); | ||
| 79 | |||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub read_next { | ||
| 84 | my $self = shift; | ||
| 85 | |||
| 86 | my $client = $self->_client; | ||
| 87 | $self->brik_help_run_undef_arg('connect', $client) or return; | ||
| 88 | |||
| 89 | my $line; | ||
| 90 | eval { | ||
| 91 | $line = $client->getline; | ||
| 92 | }; | ||
| 93 | if ($@) { | ||
| 94 | chomp($@); | ||
| 95 | return $self->log->error("read_next: getline failed with: [$@]"); | ||
| 96 | } | ||
| 97 | |||
| 98 | return $line; | ||
| 99 | } | ||
| 100 | |||
| 101 | 1; | ||
| 102 | |||
| 103 | __END__ | ||