[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.13 and 1.28

version 1.13, 2005/12/30 01:02:41 version 1.28, 2007/02/02 22:37:24
Line 1 
Line 1 
 #!/usr/bin/perl  #!/usr/bin/perl
 # $RedRiver: update_trango.pl,v 1.12 2005/12/29 18:41:17 andrew Exp $  # $RedRiver: update_trango.pl,v 1.27 2007/02/02 21:36:00 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::TFTP;  use YAML qw/ LoadFile /;
 use YAML;  use Net::TFTP;
 use lib '.';  use lib '.';
 use Net::Telnet::Trango;  use Net::Telnet::Trango;
   use RedRiver::Wireless;
 my $config_file = shift || 'update_trango.conf';  
 my $max_tries = 3;  my $config_file = shift || 'update_trango.yaml';
   my $max_tries = 3;
   
   my $l = Mylogger->new( { log_prefix => 'UT' } );
 my $l = Mylogger->new( { log_prefix => 'UT' } );  
   $l->sp("Reading config file '$config_file'");
   my $conf = LoadFile($config_file);
 $l->sp("Reading config file '$config_file'");  
 my $conf = read_conf($config_file);  my $hosts;
   if (@ARGV) {
 $l->sp("  Hardware Type: $conf->{'type'}");    @{ $hosts } = map { { name => $_, group => 'Trango-Client' } } @ARGV
 $l->sp("  File Name:     $conf->{'file_name'}");  } else {
 $l->sp("  File Size:     $conf->{'file_size'}");    $l->sp("Reading hosts");
 $l->sp("  File Checksum: $conf->{'file_cksum'}");    $hosts = RedRiver::Wireless::Read_Hosts();
 $l->sp("  FW Version:    $conf->{'ver'}");  }
 $l->sp("  FW Checksum:   $conf->{'cksum'}");  
 $l->sp("");  #@{ $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 $fox (@{ $conf->{'ips'} }) {  
   $l->sp("Updating: $fox");  foreach my $host (@{ $hosts }) {
     next if $host->{group} !~ /^Trango/;
   ## Connect and login.  
   my $t = new Net::Telnet::Trango ({    if (! exists $host->{retry}) {
     Host    => $fox,      $host->{tries} = 0;
     Timeout => 5,      $host->{retry} = 1;
     Errmode => 'return',    }
   }) or die "Couldn't make new connection: $!";  
     if ($host->{tries} >= $max_tries) {
   $l->p("Connecting to $fox");      $host->{retry} = 0;
   unless ($t->open) {    }
     $l->sp("Error connecting: $!");  
     next;    if ($host->{retry} <= 0) {
   }      next;
     }
   if ($t->host_type ne $conf->{'type'}) {  
     $l->sp("Wrong type of unit ('$t->host_type' should be '$conf->{'type'}')");    $host->{tries}++;
     $t->close;    $processed++;
     next;  
   }    $l->sp("");
     $l->sp("Checking: $host->{name} (try $host->{tries})");
   if ($t->firmware_version eq $conf->{'ver'}) {    my $needs_reboot = 0;
     $l->sp("Already up to date with firmware version '$t->firmware_version'");  
     $t->close;    ## Connect and login.
     next;    my $t = new Net::Telnet::Trango (
   }      Timeout => 5,
       Errmode => 'return',
   $l->p("Logging in");    ) or die "Couldn't make new connection: $!";
   $t->login($conf->{'password'}) || die "Couldn't login: $!";    $l->p("Connecting to $host->{name}");
     unless ( $t->open($host->{name}) ) {
   $l->p("Sending commands");      $l->sp("Error connecting: $!");
   ## Send commands      next;
   print Dump $t->login_banner;    }
   print Dump $t->sysinfo;  
   print Dump $t->last_lines;    my $password = $host->{Telnet_Password} || $conf->{general}->{password};
   print Dump $t->ver();  
   print Dump $t->tftpd();    # XXX I am not sure this is the best way to check if we should look for
   print Dump $t->enable_tftpd();    # XXX associated stations.
   print Dump [ "Exit", $t->exit ];    if ($host->{group} !~ /Client$/) {
   print Dump $t->tftpd();      $l->p("Logging in");
   print Dump $t->disable_tftpd();      $t->login($password);
   print Dump $t->tftpd();      unless ($t->logged_in) {
   #if ( upload($t, $conf->{'file_name'}) ) {        $l->p('Failed!');
   #   $l->p("Rebooting");        $t->close;
   #  $t->reboot;        next;
   #} else {      }
     $l->p("Exiting");  
     $t->exit;      $l->sp("Getting sudb");
   #}      my $sudb = $t->sudb_view;
         if ($sudb) {
   $l->sp("");        foreach my $su (@{ $sudb }) {
 }          $l->p("Getting su info $su->{suid}");
           my $su_info = $t->su_info( $su->{suid} );
 sub upload          if ($su_info->{ip}) {
 {            if (grep { $_->{name} eq $su_info->{'ip'} } @{ $hosts }) {
   my $t    = shift;              $l->p("Already have $su_info->{ip}");
   my $file = shift;              next;
             }
   my $ver = $t->ver;            $l->sp("Adding host $su_info->{ip}");
   $l->p("Current version '$ver->{'Firmware Version'}'");            my $new_host = {
               Telnet_Password => $host->{Telnet_Password},
   if (              group   => $host->{group} . '-Client',
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&              name    => $su_info->{ip},
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}              remarks => $su_info->{remarks},
   ) {            };
     $l->sp("Already updated!");            push @{ $hosts }, $new_host;
     return 1;          } else {
   }            $l->sp("Couldn't get su info for $su->{suid}");
             if ($su_info->{ERR}) {
   my $try = 0;              $l->sp("ERR: $su_info->{ERR}");
   while (1) {            }
     if ($try >= $max_tries) {          }
       $l->sp("Couldn't update in $max_tries tries!");        }
       return undef;      }
     }    }
     $try++;  
     foreach my $firmware_type ('Firmware', 'FPGA') {
     #sysinfo($self->{'_host'});  
           if (! exists $conf->{$firmware_type}) {
     $l->p("Enabling TFTPd");        $l->s("No configs for '$firmware_type'");
     $t->enable_tftpd || die "Couldn't enable tftpd";        $t->close;
         next;
     $l->p("Uploading file ($file)");      }
     # use tftp to push the file up  
     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');      my $host_type = $t->host_type;
       if ($firmware_type eq 'FPGA') {
     $tftp->put($file, $file)        $host_type =~ s/\s.*$//;
       or die "Error uploading: " . $tftp->error;      }
   
     # waitfor some sort of output      if (! exists $conf->{$firmware_type}->{$host_type}) {
     # make sure it says 'Success.' otherwise error        $l->sp("No '$firmware_type' config for type $host_type");
     #print "LAST: " . $self->lastline;        $t->close;
     #my @lines = $self->getlines;        next;
     #print Dump \@lines;      }
       
     $l->p("Checking upload ($conf->{'file_cksum'})");      if ($firmware_type eq 'Firmware' &&
     my $results = $t->tftpd;        $t->firmware_version eq
     # check the 'File Length' against ???        $conf->{$firmware_type}->{$host_type}->{ver}
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {      ) {
       $l->sp(        $l->sp("Firmware already up to date");
         "File checksum '" . $results->{'File Checksum'} .        next;
         "does not match config file '" . $conf->{'file_cksum'} . "'!"      }
       );  
       next;      if (! $t->logged_in) {
     }        $l->p("Logging in");
     $l->p("File checksum matches . . . ");        $t->login($password);
         unless ($t->logged_in) {
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {          $l->p('Failed!');
       $l->sp(          $t->close;
         "File length '" . $results->{'File Length'} .          next;
         "does not match config file '" . $conf->{'file_size'} . " bytes'!"        }
       );      }
       next;  
     }      foreach my $k (keys %{ $conf->{general} }) {
     $l->p("File length matches . . . ");        $conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k};
       }
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {      $conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type;
       $l->sp(      $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
         "File name '" . $results->{'File Name'} .  
         "' does not match config file '" . $conf->{'file_name'} . "'!"      $l->sp("$host_type $firmware_type");
       );      ## Send commands
       next;      my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
     }      if ($rc) {
     $l->p("File name  matches . . . ");        $l->sp("Successfull!");
         $host->{retry}--;
     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");        $needs_reboot = 1;
     unless ($results = $t->updateflash(      } elsif (defined $rc) {
       args => 'mainimage ' . $ver->{'Firmware Checksum'} .        $l->sp("Already up to date");
               ' '          . $conf->{'cksum'},        $host->{retry}--;
       Timeout => 90,      } else {
     ) ) {        $l->sp("Failed");
       $l->sp("Couldn't update flash: $!");        $t->bye;
       next;        next;
     }      }
   
     unless (    }
       defined $results->{'Checksum'} &&  
       $results->{'Checksum'} eq $conf->{'cksum'}    if ($needs_reboot) {
     ) {      $l->sp("Rebooting $host->{name}");
       $l->sp("Saved checksum does not match config file!");      $t->reboot;
       next;    } else {
     }      $l->sp("Bye $host->{name}");
     $l->p("Uploaded checksum ($results->{'Checksum'}) " .      $t->bye();
           "matches ($conf->{'cksum'})");    }
       }
     $l->sp("Successfully updated!");  
     return 1;    if (! $processed) {
   }      $l->sp("");
 }      $l->sp("Finished.  No more hosts.");
       last;
 sub read_conf    }
 {  }
   my $file = shift;  
   my %conf;  sub upload
   my $in_ip_list = 0;  {
   open my $fh, $file or die "Couldn't open file $file: $!";    my $t    = shift;
   while (<$fh>) {    my $conf = shift;
     chomp;  
     next if /^#/;    my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
     next if /^$/;  
     if ($in_ip_list) {    my $fw_type = $conf->{firmware_type};
       s/\s+//g; # Whitespace is a no no  
     my $ver = $t->ver;
       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {  
         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);    if (! (
       } else {      $ver->{$fw_type . ' Version'}  &&
         push @{ $conf{'ips'} }, $_;      $ver->{$fw_type . ' Checksum'}
       }    )) {
     } else {      $l->sp("Error getting current version numbers");
       my ($key, $val) = split /\s+/, $_, 2;      return;
     }
       if (lc($key) eq 'ips') {  
         $in_ip_list = 1;    if (
         next;      $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
       }      $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
     ) {
       $key =~ s/^\s+//;      return 0;
       $key =~ s/\s+$//;    }
       $val =~ s/^\s+//;  
       $val =~ s/\s+$//;    $l->sp("Updating $fw_type");
     $l->sp("Config information:");
       $conf{ lc($key) } = $val;    $l->sp("  Hardware Type: $conf->{'type'}");
     }    $l->sp("  File Name:     $conf->{'file_name'}");
   }    $l->sp("  File Size:     $conf->{'file_size'}");
   close $fh;    $l->sp("  File Checksum: $conf->{'file_cksum'}");
     $l->sp("  Conf Version:  $conf->{'ver'}");
   #print Dump \%conf;    $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
   foreach (    $l->sp("  Conf Checksum: $conf->{'cksum'}");
     'password',    $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
     'file_name', 'file_size', 'file_cksum',  
     'ver', 'cksum', 'ips',    my $try = 0;
   ) {    while (1) {
     die "No $_ specified in config file!"      if ($try >= $max_tries) {
       if (not exists $conf{$_});        $l->sp("Couldn't update in $max_tries tries!");
   }        return;
       }
   #print Dump \%conf;      $try++;
   #exit;  
   return \%conf;      $l->p("Enabling TFTPd");
 }      unless ($t->enable_tftpd) {
         $l->sp("Couldn't enable tftpd");
 package Mylogger;        next;
       }
 use Fcntl ':flock'; # import LOCK_* constants  
 #use YAML;      $l->p("Uploading file ($conf->{file_name})");
 use constant LOG_PRINT => 128;      # use tftp to push the file up
 use constant LOG_SAVE  =>  64;      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
   
 DESTROY {      unless ($tftp->put($file, $file)) {
   my $self = shift;        $l->sp("Error uploading: " . $tftp->error);
   if ($self->{'MYLOG'}) {        next;
     $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");      }
     close $self->{'MYLOG'};  
   }      $l->p("Checking upload ($conf->{'file_cksum'})");
 }      my $results = $t->tftpd;
       # check the 'File Length' against ???
 sub new {      if (! (
   my $package = shift;        $results->{'File Checksum'} &&
   my $self = shift || {};        $results->{'File Length'}   &&
         $results->{'File Name'}
   $self->{'base_path'}  ||= '.';      )) {
   $self->{'log_path'}   ||= $self->{'base_path'};        $l->sp("Unable to get results of upload");
   $self->{'log_prefix'} ||= 'LOG';        next;
   $self->{'log_file'}   ||= GetLogName(      }
     $self->{'log_prefix'},      if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
     $self->{'log_path'}        $l->sp(
   );          "File checksum (" . $results->{'File Checksum'} .
   bless $self, $package;          ") does not match config file (" . $conf->{'file_cksum'} . ")!"
 }        );
         next;
 sub s      }
 {      $l->p("File checksum matches . . . ");
   my $self = shift;  
   my $m = shift;      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
   return $self->mylog($m, LOG_SAVE);        $l->sp(
 }          "File length (" . $results->{'File Length'} .
           ") does not match config file (" . $conf->{'file_size'} . " bytes)!"
 sub p        );
 {        next;
   my $self = shift;      }
   my $m = shift;      $l->p("File length matches . . . ");
   return $self->mylog($m, LOG_PRINT);  
 }      if ( uc($results->{'File Name'}) ne uc($file) ) {
         $l->sp(
 sub sp          "File name (" . $results->{'File Name'} .
 {          ") does not match config file (" . $file . ")!"
   my $self = shift;        );
   my $m = shift;        next;
   return $self->mylog($m, LOG_SAVE | LOG_PRINT);      }
 }      $l->p("File name matches . . . ");
   
 sub mylog      my $image_type = 'mainimage';
 {      if ($fw_type eq 'FPGA') {
   my $self = shift;        $image_type = 'fpgaimage';
       }
   my $thing = shift;      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
   chomp $thing;      unless ($results = $t->updateflash(
         args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
   my $which = shift;                ' '          . $conf->{'cksum'},
         Timeout => 90,
   my $MYLOG;      ) ) {
   if ($which & LOG_PRINT) {        $l->sp("Couldn't update flash: $!");
     print $thing, "\n";        next;
   }      }
   
   if ($which & LOG_SAVE) {      unless (
     if ($self->{'MYLOG'}) {        defined $results->{'Checksum'} &&
       $MYLOG = $self->{'MYLOG'};        $results->{'Checksum'} eq $conf->{'cksum'}
     } else {      ) {
       unless ($MYLOG) {        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
                 open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})        next;
           or die "Couldn't open logfile!\n";      }
         my $ofh = select $MYLOG;  
         $|=1;      $l->p("$fw_type saved checksum matches . . . ");
         select $ofh;  
         $self->{'MYLOG'} = $MYLOG;      return 1;
     }
         $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");  }
       }  
     }  package Mylogger;
     flock($MYLOG, LOCK_EX);  
     print $MYLOG (scalar gmtime), "\t", $thing, "\n"  use Fcntl ':flock'; # import LOCK_* constants
       or die "Couldn't print to MYLOG: $!";  #use YAML;
     flock($MYLOG, LOCK_UN);  use constant LOG_PRINT => 128;
   }  use constant LOG_SAVE  =>  64;
 }  
   DESTROY {
 sub GetLogName    my $self = shift;
 {    if ($self->{'MYLOG'}) {
   my $prefix  = shift || die "Invalid prefix passed for log";      $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
       close $self->{'MYLOG'};
   my $logdate = GetLogDate();    }
   my $logver  = 0;  }
   my $logname;  
   sub new {
   do {    my $package = shift;
     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';    my $self = shift || {};
     $logver++;  
   } until (not -e $logname);    $self->{'base_path'}  ||= '.';
     $self->{'log_path'}   ||= $self->{'base_path'};
   return $logname;    $self->{'log_prefix'} ||= 'LOG';
 }    $self->{'log_file'}   ||= GetLogName(
       $self->{'log_prefix'},
 sub GetLogDate      $self->{'log_path'}
 {    );
   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();    bless $self, $package;
   }
   $mon++;  
   $year += 1900;  sub s
   {
   if ($min  < 10) { $min  = "0$min"  }    my $self = shift;
   if ($sec  < 10) { $sec  = "0$sec"  }    my $m = shift;
   if ($hour < 10) { $hour = "0$hour" }    return $self->mylog($m, LOG_SAVE);
   if ($mday < 10) { $mday = "0$mday" }  }
   if ($mon  < 10) { $mon  = "0$mon"  }  
   sub p
   my $time = $year . $mon . $mday;  {
     my $self = shift;
   return $time;    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.13  
changed lines
  Added in v.1.28

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