[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.12 and 1.29

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

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