[BACK]Return to update_trango.pl CVS log [TXT][DIR] Up to [local] / trango / Net-Telnet-Trango / scripts

Diff for /trango/Net-Telnet-Trango/scripts/update_trango.pl between version 1.11 and 1.12

version 1.11, 2005/12/21 02:03:30 version 1.12, 2005/12/29 18:41:17
Line 1 
Line 1 
 #!/usr/bin/perl  #!/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  # update_trango.pl *** Updates trango foxes with a new firmware
 #  #
Line 9 
Line 9 
 use warnings;  use warnings;
   
 use Net::TFTP;  use Net::TFTP;
   use YAML;
   use lib '.';
   use Net::Telnet::Trango;
   
 my $config_file = shift || 'update_trango.conf';  my $config_file = shift || 'update_trango.conf';
 my $max_tries = 3;  my $max_tries = 3;
Line 38 
Line 41 
   $l->sp("Updating: $fox");    $l->sp("Updating: $fox");
   
   ## Connect and login.    ## Connect and login.
   my $t = new Trango::Telnet ({    my $t = new Net::Telnet::Trango ({
     Host    => $fox,      Host    => $fox,
     Timeout => 5,      Timeout => 5,
   });    });
Line 68 
Line 71 
   
   $l->p("Sending commands");    $l->p("Sending commands");
   ## Send commands    ## Send commands
     #print Dump $t->ver();
     #print Dump $t->tftpd();
   if ( upload($t, $conf->{'file_name'}) ) {    if ( upload($t, $conf->{'file_name'}) ) {
     $l->p("Rebooting");      $l->p("Rebooting");
     $t->reboot;      $t->reboot;
Line 75 
Line 80 
     $l->p("Exiting");      $l->p("Exiting");
     $t->exit;      $t->exit;
   }    }
     
   $t->close;    $t->close;
   $l->sp("");    $l->sp("");
 }  }
Line 224 
Line 230 
   #exit;    #exit;
   return \%conf;    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;  package Mylogger;
   

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.12

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>