[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.31

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

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