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

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

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