Annotation of trango/Net-Telnet-Trango/lib/Net/Telnet/Trango.pm, Revision 1.1
1.1 ! andrew 1: package Net::Telnet::Trango;
! 2: use base 'Net::Telnet';
! 3:
! 4: my %PRIVATE = (
! 5: is_connected => 0,
! 6: logged_in => 0,
! 7: );
! 8:
! 9:
! 10: sub AUTOLOAD
! 11: {
! 12: my $self = shift;
! 13:
! 14: my ($method) = (our $AUTOLOAD) =~ /^.*::(\w+)$/
! 15: or die "Weird: $AUTOLOAD";
! 16:
! 17: my $success = 'Success.';
! 18: my %MEMBERS = (
! 19: ver => {},
! 20: sysinfo => { waitfor => $success },
! 21: tftpd => { waitfor => $success },
! 22: );
! 23:
! 24: my %ACCESS = map { $_ => 1 } qw(
! 25: firmware_version
! 26: host_type
! 27: Host
! 28: is_connected
! 29: logged_in
! 30: Timeout
! 31: );
! 32:
! 33: if (exists $MEMBERS{$method}) {
! 34: return $self->cmd($method, $MEMBERS{$method}{waitfor});
! 35: }
! 36:
! 37: if (exists $ACCESS{$method}) {
! 38: my $var = shift || $PRIVATE{$method};
! 39: $PRIVATE{$method} = $var;
! 40: return $var;
! 41: }
! 42:
! 43: $method = "SUPER::$method";
! 44: return $self->$method(@_);
! 45: }
! 46:
! 47: sub new
! 48: {
! 49: my $class = shift;
! 50: my $args = shift || {};
! 51:
! 52: $args->{'Timeout'} ||= 5;
! 53: $args->{'Prompt'} ||= '/#> *$/';
! 54:
! 55: foreach my $key (keys %{ $args }) {
! 56: $PRIVATE{$key} = $args->{$key};
! 57: }
! 58:
! 59: my $self = $class->SUPER::new(%{ $args });
! 60: bless $self;
! 61:
! 62: #bless $self, $package;
! 63: return $self;
! 64: }
! 65:
! 66: sub connect
! 67: {
! 68: my $self = shift;
! 69:
! 70: unless ( $self->open(
! 71: Host => $self->Host,
! 72: Errmode => 'return',
! 73: ) ) {
! 74: $! = "Couldn't connect to $self->Host. Connection timed out.";
! 75: return undef, undef;
! 76: }
! 77: #$self->dump_log('dump.log');
! 78:
! 79: ## Login to remote host.
! 80: unless ($self->waitfor(
! 81: -match => '/password: ?$/i',
! 82: -errmode => "return",
! 83: ) ) {
! 84: $! = "problem connecting to host ($self->Host): " . $self->lastline;
! 85: return undef;
! 86: }
! 87:
! 88: $self->login_banner($self->lastline);
! 89:
! 90: $self->is_connected(1);
! 91:
! 92: return ($self->host_type, $self->firmware_version);
! 93: }
! 94:
! 95: sub login
! 96: {
! 97: my $self = shift;
! 98:
! 99: my $password = shift;
! 100:
! 101: $self->print($password);
! 102: unless ($self->waitfor(
! 103: -match => $self->prompt,
! 104: -errmode => "return",
! 105: ) ) {
! 106: $! = "login ($self->Host) failed: " . $self->lastline;
! 107: return undef;
! 108: }
! 109:
! 110: $self->logged_in(1);
! 111:
! 112: return $self->logged_in;
! 113: }
! 114:
! 115: sub login_banner
! 116: {
! 117: my $self = shift;
! 118:
! 119: my $banner = shift || $self->login_banner;
! 120:
! 121: my ($type, $ver) = $banner =~
! 122: /Welcome to Trango Broadband Wireless (\S+)[\s-]+(.+)$/i;
! 123:
! 124: $self->host_type($type);
! 125: $self->firmware_version($ver);
! 126:
! 127: return $banner;
! 128: }
! 129:
! 130: sub reboot
! 131: {
! 132: my $self = shift;
! 133:
! 134: $self->print("reboot\n");
! 135: $self->getline;
! 136:
! 137: return 1;
! 138: }
! 139:
! 140: sub exit
! 141: {
! 142: my $self = shift;
! 143:
! 144: $self->print("exit\n");
! 145: $self->getline;
! 146:
! 147: return 1;
! 148: }
! 149:
! 150: sub enable_tftpd
! 151: {
! 152: my $self = shift;
! 153:
! 154: my $vals = $self->cmd('tftpd on', 'Success.');
! 155:
! 156: if ($vals->{'Tftpd'} eq 'listen') {
! 157: return $vals;
! 158: } else {
! 159: return undef;
! 160: }
! 161: }
! 162:
! 163: sub updateflash
! 164: {
! 165: my $self = shift;
! 166:
! 167: my $old = shift;
! 168: my $new = shift;
! 169:
! 170: return undef unless $new;
! 171:
! 172: return $self->cmd("updateflash mainimage $old $new", 'Success.', 90);
! 173: }
! 174:
! 175: sub cmd
! 176: {
! 177: my $self = shift;
! 178:
! 179: my $string = shift;
! 180: my $expect_last = shift;
! 181: my $timeout = shift || $self->Timeout;
! 182:
! 183: unless (defined $string) {
! 184: $! = "No command passed";
! 185: return undef;
! 186: }
! 187:
! 188: unless ($self->is_connected) {
! 189: $! = "Not connected";
! 190: return undef;
! 191: }
! 192:
! 193: unless ($self->logged_in) {
! 194: $! = "Not logged in";
! 195: return undef;
! 196: }
! 197:
! 198: my @lines = $self->SUPER::cmd(String => $string, Timeout => $timeout);
! 199:
! 200: my $vals = _decode_lines(@lines);
! 201:
! 202: unless ($expect_last) {
! 203: return $vals;
! 204: }
! 205:
! 206: my $last = $self->lastline;
! 207:
! 208: if ($last =~ /$expect_last$/) {
! 209: return $vals;
! 210: } else {
! 211: warn "Error with command ($string): $last";
! 212: return undef;
! 213: }
! 214: }
! 215:
! 216: sub _decode_lines
! 217: {
! 218: my @lines = @_;
! 219:
! 220: my %conf;
! 221:
! 222: my $key = '';
! 223: my $val = '';
! 224: my $in_key = 0;
! 225: my $in_val = 0;
! 226:
! 227: foreach my $line (@lines) {
! 228: my @chars = split //, $line;
! 229:
! 230: my $last_key = '';
! 231: foreach my $c (@chars) {
! 232:
! 233: if ($c eq '[' || $c eq "\r" || $c eq "\n") {
! 234: if ($c eq '[') {
! 235: $in_key = 1;
! 236: $in_val = 0;
! 237: } else {
! 238: $in_key = 0;
! 239: $in_val = 0;
! 240: }
! 241:
! 242: if ($key) {
! 243: $key =~ s/^\s+//;
! 244: $key =~ s/\s+$//;
! 245:
! 246: $val =~ s/^\s+//;
! 247: $val =~ s/\s+$//;
! 248:
! 249: if ($key eq 'Checksum' && $last_key) {
! 250: # Special case for these bastids.
! 251: my $new = $last_key;
! 252: $new =~ s/\s+\S+$//;
! 253: $key = $new . " " . $key;
! 254: }
! 255:
! 256: $last_key = $key;
! 257: $conf{$key} = $val;
! 258: $key = '';
! 259: $val = '';
! 260: }
! 261:
! 262: } elsif ($c eq ']') {
! 263: $in_val = 1;
! 264: $in_key = 0;
! 265: $c = shift @chars;
! 266:
! 267: } elsif ($in_key) {
! 268: $key .= $c;
! 269:
! 270: } elsif ($in_val) {
! 271: $val .= $c;
! 272: }
! 273: }
! 274: }
! 275: #print Dump \%conf;
! 276:
! 277: if (%conf) {
! 278: return \%conf;
! 279: } else {
! 280: return \@lines;
! 281: }
! 282: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>