[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.3 and 1.32

version 1.3, 2005/11/16 20:48:48 version 1.32, 2007/02/07 19:25:05
Line 1 
Line 1 
 #!/usr/bin/perl  #!/usr/bin/perl
 # $RedRiver: update_trango.pl,v 1.2 2005/11/16 05:26:37 andrew Exp $  # $RedRiver: update_trango.pl,v 1.31 2007/02/06 23:15:53 andrew Exp $
 ########################################################################  ########################################################################
 # update_trango.pl *** Updates trango foxes with a new firmware  # update_trango.pl *** Updates trango hosts with a new firmware
 #  #
 # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org>  # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org>
 ########################################################################  ########################################################################
 use strict;  # Copyright (C) 2005, 2006, 2007 by Andrew Fresh
 use warnings;  #
   # This program is free software; you can redistribute it and/or modify
 use Net::Telnet;  # it under the same terms as Perl itself.
 use Net::TFTP;  ########################################################################
 #use YAML;  use strict;
   use warnings;
   
 my $config_file = shift || 'fox53.conf';  use YAML qw/ LoadFile /;
 my $conf = read_conf($config_file);  use Net::TFTP;
   use Net::Telnet::Trango;
 my $max_tries = 1;  
   my $config_file = shift || 'update_trango.yaml';
   my $max_tries = 3;
   
   my $l = Mylogger->new( { log_prefix => 'UT' } );
   
   $l->sp("Reading config file '$config_file'");
 foreach my $fox (@{ $conf->{'ips'} }) {  my $conf = LoadFile($config_file);
   print STDERR "Updating: $fox\n";  
   my $hosts;
   ## Connect and login.  if (@ARGV) {
   my $host = new Net::Telnet (Timeout => 5,      @{ $hosts } = map { { name => $_, group => 'Trango-Client' } } @ARGV
                               Prompt => '/#> *$/');  } else {
   print "Connecting to $fox\n";      $hosts = parse_hosts($conf->{hosts});
   $host->open($fox);  }
   $host->dump_log('dump.log');  
   #@{ $hosts } = grep { $_->{name} eq '10.100.7.2' } @{ $hosts };
   ## Login to remote host.  
   $host->waitfor(  my $global_tries = $max_tries * 2;
     -match => '/password: ?$/i',  while ($global_tries > 0) {
     -errmode => "return",      $global_tries--;
   ) or die "problem connecting to host ($fox): ", $host->lastline;      my $processed = 0;
   
   my $login_banner = $host->lastline;      foreach my $host (@{ $hosts }) {
   my ($type, $version) = $login_banner =~  
     /Welcome to Trango Broadband Wireless (\w+)-(.+)$/i;          if (! exists $host->{retry}) {
               $host->{tries} = 0;
   if ($type ne $conf->{'type'}) {              $host->{retry} = 1;
     print STDERR "Wrong type of unit ('$type' should be '$conf->{'type'}')\n";          }
     $host->close;  
     next;          if ($host->{tries} >= $max_tries) {
   }              $host->{retry} = 0;
           }
   if ($version eq $conf->{'ver'}) {  
     print STDERR "Already up to date with firmware version '$version'\n";          if ($host->{retry} <= 0) {
     $host->close;              next;
     next;          }
   }  
           $host->{tries}++;
   print "Logging in\n";          $processed++;
   $host->print($conf->{'password'});  
   $host->waitfor(          $l->sp("");
     -match => $host->prompt,          $l->sp("Checking: $host->{name} (try $host->{tries})");
     -errmode => "return",          my $needs_reboot = 0;
   ) or die "login ($fox) failed: ", $host->lastline;  
           ## Connect and login.
           my $t = new Net::Telnet::Trango (
   print "Sending commands\n";              Timeout => 5,
   ## Send commands              Errmode => 'return',
   if ( upload($host, $fox, $conf->{'file_name'}) ) {          ) or die "Couldn't make new connection: $!";
     print "Rebooting\n";          $l->p("Connecting to $host->{name}");
     $host->print("reboot\n");          unless ( $t->open($host->{name}) ) {
     $host->getline;              $l->sp("Error connecting: $!");
   } else {              next;
     print "Exiting\n";          }
     $host->print("exit\n");  
     $host->getline;          my $password = $host->{Telnet_Password} || $conf->{general}->{password};
   }  
   $host->close;          $l->p("Logging in");
 }          $t->login($password);
           unless ($t->logged_in) {
 sub upload              $l->p('Failed!');
 {              $t->close;
   my $host = shift;              next;
   my $fox  = shift;          }
   my $file = shift;  
           $l->sp("Getting sudb");
   print "Getting current version\n";          my $sudb = $t->sudb_view;
   my $ver = get_ver($host);          if ($sudb) {
               foreach my $su (@{ $sudb }) {
   if (                  $l->p("Getting su info $su->{suid}");
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&                  my $su_info = $t->su_info( $su->{suid} );
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}                  if ($su_info->{ip}) {
   ) {                      if (grep { $_->{name} eq $su_info->{'ip'} } @{ $hosts }) {
     print STDERR "Already updated!";                          $l->p("Already have $su_info->{ip}");
     return 1;                          next;
   }                      }
                       $l->sp("Adding host $su_info->{ip}");
   my $try = 0;                      my $new_host = {
   while (1) {                          password => $host->{password},
     if ($try >= $max_tries) {                          name     => $su_info->{ip},
       print STDERR "Couldn't update in $max_tries tries!";                          remarks  => $su_info->{remarks},
       return undef;                      };
     }                      push @{ $hosts }, $new_host;
     $try++;                  } else {
                       $l->sp("Couldn't get su info for $su->{suid}");
     #sysinfo($host);                      $l->sp("ERR: " . $t->last_error);
                       }
     print "Enabling TFTPd\n";              }
     enable_tftpd($host) || die "Couldn't enable tftpd";          }
   
     print "Uploading file\n";          foreach my $firmware_type ('Firmware', 'FPGA') {
     # use tftp to push the file up  
     my $tftp = Net::TFTP->new($fox, Mode => 'octet');              if (! exists $conf->{$firmware_type}) {
                   $l->s("No configs for '$firmware_type'");
     $tftp->put($file, $file)                  next;
       or die "Error uploading: " . $tftp->error;              }
   
     # waitfor some sort of output              my $host_type = $t->host_type;
     # make sure it says 'Success.' otherwise error              if ($firmware_type eq 'FPGA') {
     #print "LAST: " . $host->lastline;                  $host_type =~ s/\s.*$//;
     #my @lines = $host->getlines;              }
     #print Dump \@lines;  
                   if (! exists $conf->{$firmware_type}->{$host_type}) {
     print "Checking upload\n";                  $l->sp("No '$firmware_type' config for type $host_type");
     my $results = check_tftpd($host);                  next;
     # check the 'File Length' against ???              }
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {  
       print STDERR "File checksum does not match config file!";              if ($firmware_type eq 'Firmware' &&
       next;                  $t->firmware_version eq
     }                  $conf->{$firmware_type}->{$host_type}->{ver}
               ) {
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {                  $l->sp("Firmware already up to date");
       print STDERR "File length does not match config file!";                  next;
       next;              }
     }  
               if (! $t->logged_in) {
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {                  $l->p("Logging in");
       print STDERR "File name does not match config file!";                  $t->login($password);
       next;                  unless ($t->logged_in) {
     }                      $l->p('Failed!');
                       $t->close;
     print "Updating flash\n";                      last;
     $results = updateflash(                  }
       $host, $ver->{'Firmware Checksum'}, $conf->{'cksum'}              }
     ) or die "Couldn't update flash: " . $host->lastline;  
     unless (              foreach my $k (keys %{ $conf->{general} }) {
       defined $results->{'Checksum'} &&                  $conf->{$firmware_type}->{$host_type}->{$k}
       $results->{'Checksum'} eq $conf->{'cksum'}                    ||= $conf->{general}->{$k};
     ) {              }
       print STDERR "Saved checksum does not match config file!";              $conf->{$firmware_type}->{$host_type}->{firmware_type}
       next;                ||= $firmware_type;
     }              $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
       
     print STDERR "Successfully updated!\n";              $l->sp("$host_type $firmware_type");
     return 1;              ## Send commands
   }              my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
 }              if ($rc) {
                   $l->sp("Successfull!");
 sub get_ver                  $host->{retry}--;
 {                  $needs_reboot = 1;
   my $host = shift;              } elsif (defined $rc) {
   return cmd($host, 'ver');                  $l->sp("Already up to date");
 }                  $host->{retry}--;
               } else {
 sub enable_tftpd                  $l->sp("Failed");
 {                  $t->bye;
   my $host = shift;                  next;
               }
   my $vals = cmd($host, 'tftpd on', 'Success.');  
           }
   if ($vals->{'Tftpd'} eq 'listen') {  
     return $vals;          if ($needs_reboot) {
   } else {              $l->sp("Rebooting $host->{name}");
     return undef;              $t->reboot;
   }          } else {
 }              $l->sp("Bye $host->{name}");
               $t->bye();
 sub check_tftpd          }
 {      }
   my $host = shift;  
   return cmd($host, 'tftpd', 'Success.');      if (! $processed) {
 }          $l->sp("");
           $l->sp("Finished.  No more hosts.");
 sub sysinfo          last;
 {      }
   my $host = shift;  }
   return cmd($host, 'sysinfo', 'Success.');  
 }  sub upload
   {
 sub updateflash      my $t    = shift;
 {      my $conf = shift;
   my $host = shift;  
   my $old = shift;      my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
   my $new = shift;  
       my $fw_type = $conf->{firmware_type};
   return undef unless $new;  
       my $ver = $t->ver;
   return cmd($host, "updateflash mainimage $old $new", 'Success.', 90);  
 }      if (! (
               $ver->{$fw_type . ' Version'}  &&
 sub cmd              $ver->{$fw_type . ' Checksum'}
 {          )) {
   my $host   = shift;          $l->sp("Error getting current version numbers");
   my $string = shift;          return;
   my $expect_last = shift;      }
   my $timeout = shift || 5;  
       if (
   my @lines = $host->cmd(String => $string, Timeout => $timeout);          $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
           $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
   my $vals = decode_lines(@lines);      ) {
           return 0;
   my $last = $host->lastline;      }
   
   unless ($expect_last) {      $l->sp("Updating $fw_type");
     return $vals;      $l->sp("Config information:");
   }      $l->sp("  Hardware Type: $conf->{'type'}");
       $l->sp("  File Name:     $conf->{'file_name'}");
   if ($last =~ /$expect_last$/) {      $l->sp("  File Size:     $conf->{'file_size'}");
     return $vals;      $l->sp("  File Checksum: $conf->{'file_cksum'}");
   } else {      $l->sp("  Conf Version:  $conf->{'ver'}");
     warn "Error with command ($string): $last";      $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
     return undef;      $l->sp("  Conf Checksum: $conf->{'cksum'}");
   }      $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
 }  
       my $try = 0;
 sub decode_lines      while (1) {
 {          if ($try >= $max_tries) {
   my @lines = @_;              $l->sp("Couldn't update in $max_tries tries!");
               return;
   my %conf;          }
           $try++;
   my $key = '';  
   my $val = '';          $l->p("Enabling TFTPd");
   my $in_key = 0;          unless ($t->enable_tftpd) {
   my $in_val = 0;              $l->sp("Couldn't enable tftpd");
               next;
   foreach my $line (@lines) {          }
     my @chars = split //, $line;  
           $l->p("Uploading file ($conf->{file_name})");
     my $last_key = '';          # use tftp to push the file up
     foreach my $c (@chars) {          my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
   
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {          unless ($tftp->put($file, $file)) {
         if ($c eq '[') {              $l->sp("Error uploading: " . $tftp->error);
           $in_key = 1;              next;
           $in_val = 0;          }
         } else {  
           $in_key = 0;          $l->p("Checking upload ($conf->{'file_cksum'})");
           $in_val = 0;          my $results = $t->tftpd;
         }          # check the 'File Length' against ???
           if (! (
         if ($key) {                  $results->{'File Checksum'} &&
           $key =~ s/^\s+//;                  $results->{'File Length'}   &&
           $key =~ s/\s+$//;                  $results->{'File Name'}
               )) {
           $val =~ s/^\s+//;              $l->sp("Unable to get results of upload");
           $val =~ s/\s+$//;              next;
           }
           if ($key eq 'Checksum' && $last_key) {          if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
             # Special case for these bastids.              $l->sp(
             my $new = $last_key;                  "File checksum (" . $results->{'File Checksum'} .
             $new =~ s/\s+\S+$//;                  ") does not match config file (" . $conf->{'file_cksum'} . ")!"
             $key = $new . " " . $key;              );
           }              next;
           }
           $last_key = $key;          $l->p("File checksum matches . . . ");
           $conf{$key} = $val;  
           $key = '';          if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
           $val = '';              $l->sp(
         }                  "File length (" . $results->{'File Length'} .
                   ") does not match config file (" . $conf->{'file_size'} . " bytes)!"
       } elsif ($c eq ']') {              );
         $in_val = 1;              next;
         $in_key = 0;          }
         $c = shift @chars;          $l->p("File length matches . . . ");
   
       } elsif ($in_key) {          if ( uc($results->{'File Name'}) ne uc($file) ) {
         $key .= $c;              $l->sp(
                   "File name (" . $results->{'File Name'} .
       } elsif ($in_val) {                  ") does not match config file (" . $file . ")!"
         $val .= $c;              );
       }              next;
     }          }
   }          $l->p("File name matches . . . ");
   #print Dump \%conf;  
           my $image_type = 'mainimage';
   if (%conf) {          if ($fw_type eq 'FPGA') {
     return \%conf;              $image_type = 'fpgaimage';
   } else {          }
     return \@lines;          $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
   }          unless ($results = $t->updateflash(
 }                  args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
                   ' '          . $conf->{'cksum'},
 sub read_conf                  Timeout => 90,
 {              ) ) {
   my $file = shift;              $l->sp("Couldn't update flash: $!");
   my %conf;              next;
   my $in_ip_list = 0;          }
   open my $fh, $file or die "Couldn't open file $file: $!";  
   while (<$fh>) {          unless (
     chomp;              defined $results->{'Checksum'} &&
     next if /^#/;              $results->{'Checksum'} eq $conf->{'cksum'}
     next if /^$/;          ) {
     if ($in_ip_list) {              $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
       push @{ $conf{'ips'} }, $_;              next;
     } else {          }
       my ($key, $val) = split /\s+/, $_, 2;  
           $l->p("$fw_type saved checksum matches . . . ");
       if (lc($key) eq 'ips') {  
         $in_ip_list = 1;          return 1;
         next;      }
       }  }
   
       $conf{ lc($key) } = $val;  sub parse_hosts
     }  {
   }      my $src = shift;
   close $fh;  
       my @hosts;
   #print Dump \%conf;      foreach my $h (@{ $src }) {
   foreach (          if ($h->{name} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {
     'password',              for ($2..$3) {
     'file_name', 'file_size', 'file_cksum',                  my %cur_host;
     'ver', 'cksum', 'ips',                  foreach my $k (keys %{ $h }) {
   ) {                      $cur_host{$k} = $h->{$k};
     die "No $_ specified in config file!"                  }
       if (not exists $conf{$_});                  $cur_host{name} = $1 . $_;
   }                  if (! grep { $cur_host{name} eq $h->{name} } @hosts) {
                       push @hosts, \%cur_host;
   return \%conf;                  }
 }              }
           } else {
               push @hosts, $h;
           }
       }
   
       return \@hosts;
   }
   
   package Mylogger;
   
   use Fcntl ':flock'; # import LOCK_* constants
   #use YAML;
   use constant LOG_PRINT => 128;
   use constant LOG_SAVE  =>  64;
   
   DESTROY {
       my $self = shift;
       if ($self->{'MYLOG'}) {
           $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
           close $self->{'MYLOG'};
       }
   }
   
   sub new {
       my $package = shift;
       my $self = shift || {};
   
       $self->{'base_path'}  ||= '.';
       $self->{'log_path'}   ||= $self->{'base_path'};
       $self->{'log_prefix'} ||= 'LOG';
       $self->{'log_file'}   ||= GetLogName(
           $self->{'log_prefix'},
           $self->{'log_path'}
       );
       bless $self, $package;
   }
   
   sub s
   {
       my $self = shift;
       my $m = shift;
       return $self->mylog($m, LOG_SAVE);
   }
   
   sub p
   {
       my $self = shift;
       my $m = shift;
       return $self->mylog($m, LOG_PRINT);
   }
   
   sub sp
   {
       my $self = shift;
       my $m = shift;
       return $self->mylog($m, LOG_SAVE | LOG_PRINT);
   }
   
   sub mylog
   {
       my $self = shift;
   
       my $thing = shift;
       chomp $thing;
   
       my $which = shift;
   
       my $MYLOG;
       if ($which & LOG_PRINT) {
           print $thing, "\n";
       }
   
       if ($which & LOG_SAVE) {
           if ($self->{'MYLOG'}) {
               $MYLOG = $self->{'MYLOG'};
           } else {
               unless ($MYLOG) {
                   open ($MYLOG, '>>', $self->{'log_path'} . '/' .
                       $self->{'log_file'}) or die "Couldn't open logfile!\n";
                   my $ofh = select $MYLOG;
                   $|=1;
                   select $ofh;
                   $self->{'MYLOG'} = $MYLOG;
   
                   $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
               }
           }
           flock($MYLOG, LOCK_EX);
           print $MYLOG (scalar gmtime), "\t", $thing, "\n"
               or die "Couldn't print to MYLOG: $!";
           flock($MYLOG, LOCK_UN);
       }
   }
   
   sub GetLogName
   {
       my $prefix  = shift || die "Invalid prefix passed for log";
   
       my $logdate = GetLogDate();
       my $logver  = 0;
       my $logname;
   
       do {
           $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
           $logver++;
       } until (not -e $logname);
   
       return $logname;
   }
   
   sub GetLogDate
   {
       my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
   
       $mon++;
       $year += 1900;
   
       if ($min  < 10) { $min  = "0$min"  }
       if ($sec  < 10) { $sec  = "0$sec"  }
       if ($hour < 10) { $hour = "0$hour" }
       if ($mday < 10) { $mday = "0$mday" }
       if ($mon  < 10) { $mon  = "0$mon"  }
   
       my $time = $year . $mon . $mday;
   
       return $time;
   }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.32

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