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

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

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