=================================================================== RCS file: /cvs/trango/Net-Telnet-Trango/scripts/update_trango.pl,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- trango/Net-Telnet-Trango/scripts/update_trango.pl 2005/12/21 02:03:30 1.11 +++ trango/Net-Telnet-Trango/scripts/update_trango.pl 2005/12/29 18:41:17 1.12 @@ -1,5 +1,5 @@ #!/usr/bin/perl -# $RedRiver: update_trango.pl,v 1.10 2005/12/21 01:17:06 andrew Exp $ +# $RedRiver: update_trango.pl,v 1.11 2005/12/21 02:03:30 andrew Exp $ ######################################################################## # update_trango.pl *** Updates trango foxes with a new firmware # @@ -9,6 +9,9 @@ use warnings; use Net::TFTP; +use YAML; +use lib '.'; +use Net::Telnet::Trango; my $config_file = shift || 'update_trango.conf'; my $max_tries = 3; @@ -38,7 +41,7 @@ $l->sp("Updating: $fox"); ## Connect and login. - my $t = new Trango::Telnet ({ + my $t = new Net::Telnet::Trango ({ Host => $fox, Timeout => 5, }); @@ -68,6 +71,8 @@ $l->p("Sending commands"); ## Send commands + #print Dump $t->ver(); + #print Dump $t->tftpd(); if ( upload($t, $conf->{'file_name'}) ) { $l->p("Rebooting"); $t->reboot; @@ -75,6 +80,7 @@ $l->p("Exiting"); $t->exit; } + $t->close; $l->sp(""); } @@ -224,304 +230,6 @@ #exit; return \%conf; } - -package Trango::Telnet; -use base 'Net::Telnet'; - -my %PRIVATE = ( - connected => 0, - logged_in => 0, -); - -sub new { - my $class = shift; - my $args = shift || {}; - - $args->{'Timeout'} ||= 5; - $args->{'Prompt'} ||= '/#> *$/'; - - foreach my $key (keys %{ $args }) { - $PRIVATE{$key} = $args->{$key}; - } - - my $self = $class->SUPER::new(%{ $args }); - bless $self; - - #bless $self, $package; - return $self; -} - -sub connect -{ - my $self = shift; - - unless ( $self->open( - Host => $PRIVATE{'Host'}, - Errmode => 'return', - ) ) { - $! = "Couldn't connect to $self->{'Host'}. Connection timed out."; - return undef, undef; - } - #$self->dump_log('dump.log'); - - ## Login to remote host. - unless ($self->waitfor( - -match => '/password: ?$/i', - -errmode => "return", - ) ) { - $! = "problem connecting to host ($self->{'Host'}): " . $self->lastline; - return undef; - } - - $self->login_banner($self->lastline); - - $PRIVATE{'connected'} = 1; - - return ($self->host_type, $self->firmware_version); -} - -sub login -{ - my $self = shift; - - my $password = shift; - - $self->print($password); - unless ($self->waitfor( - -match => $self->prompt, - -errmode => "return", - ) ) { - $! = "login ($self->{'Host'}) failed: " . $self->lastline; - return undef; - } - - $PRIVATE{'logged_in'} = 1; - - return 1; -} - -sub login_banner -{ - my $self = shift; - - my $banner = shift || $PRIVATE{'login_banner'}; - - my ($type, $ver) = $banner =~ - /Welcome to Trango Broadband Wireless (\S+)[\s-]+(.+)$/i; - - $self->host_type($type); - $self->firmware_version($ver); - - return $banner; -} - -sub host_type -{ - my $self = shift; - - my $type = shift || $PRIVATE{'host_type'}; - $PRIVATE{'host_type'} = $type; - - return $type; -} - -sub firmware_version -{ - my $self = shift; - - my $ver = shift || $PRIVATE{'firmware_version'}; - $PRIVATE{'firmware_version'} = $ver; - - return $ver -} - -sub Host -{ - my $self = shift; - - my $host = shift || $PRIVATE{'Host'}; - $PRIVATE{'HOST'} = $host; - - return $host; -} - - -sub reboot -{ - my $self = shift; - - $self->print("reboot\n"); - $self->getline; - - return 1; -} - -sub exit -{ - my $self = shift; - - $self->print("exit\n"); - $self->getline; - - return 1; -} - -sub ver -{ - my $self = shift; - - return $self->cmd('ver'); -} - -sub tftpd -{ - my $self = shift; - - return $self->cmd('tftpd', 'Success.'); -} - -sub sysinfo -{ - my $self = shift; - - return $self->cmd('sysinfo', 'Success.'); -} - -sub enable_tftpd -{ - my $self = shift; - - my $vals = $self->cmd('tftpd on', 'Success.'); - - if ($vals->{'Tftpd'} eq 'listen') { - return $vals; - } else { - return undef; - } -} - -sub updateflash -{ - my $self = shift; - - my $old = shift; - my $new = shift; - - return undef unless $new; - - return $self->cmd("updateflash mainimage $old $new", 'Success.', 90); -} - -sub cmd -{ - my $self = shift; - - my $string = shift; - my $expect_last = shift; - my $timeout = shift || $PRIVATE{'Timeout'}; - - unless (defined $string) { - $! = "No command passed"; - return undef; - } - - unless ($PRIVATE{'connected'}) { - $! = "Not connected"; - return undef; - } - - unless ($PRIVATE{'logged_in'}) { - $! = "Not logged in"; - return undef; - } - - my @lines = $self->SUPER::cmd(String => $string, Timeout => $timeout); - - my $vals = _decode_lines(@lines); - - unless ($expect_last) { - return $vals; - } - - my $last = $self->lastline; - - if ($last =~ /$expect_last$/) { - return $vals; - } else { - warn "Error with command ($string): $last"; - return undef; - } -} - -sub _decode_lines -{ - my @lines = @_; - - my %conf; - - my $key = ''; - my $val = ''; - my $in_key = 0; - my $in_val = 0; - - foreach my $line (@lines) { - my @chars = split //, $line; - - my $last_key = ''; - foreach my $c (@chars) { - - if ($c eq '[' || $c eq "\r" || $c eq "\n") { - if ($c eq '[') { - $in_key = 1; - $in_val = 0; - } else { - $in_key = 0; - $in_val = 0; - } - - if ($key) { - $key =~ s/^\s+//; - $key =~ s/\s+$//; - - $val =~ s/^\s+//; - $val =~ s/\s+$//; - - if ($key eq 'Checksum' && $last_key) { - # Special case for these bastids. - my $new = $last_key; - $new =~ s/\s+\S+$//; - $key = $new . " " . $key; - } - - $last_key = $key; - $conf{$key} = $val; - $key = ''; - $val = ''; - } - - } elsif ($c eq ']') { - $in_val = 1; - $in_key = 0; - $c = shift @chars; - - } elsif ($in_key) { - $key .= $c; - - } elsif ($in_val) { - $val .= $c; - } - } - } - #print Dump \%conf; - - if (%conf) { - return \%conf; - } else { - return \@lines; - } -} - - package Mylogger;