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

version 1.13, 2005/12/30 01:02:41 version 1.27, 2007/02/02 21:36:00
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.25 2007/02/02 17:50:09 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) {            next;
       $l->sp("Couldn't update in $max_tries tries!");          }
       return undef;        }
     }      }
     $try++;    }
   
     #sysinfo($self->{'_host'});    foreach my $firmware_type ('Firmware', 'FPGA') {
       
     $l->p("Enabling TFTPd");      if (! exists $conf->{$firmware_type}) {
     $t->enable_tftpd || die "Couldn't enable tftpd";        $l->s("No configs for '$firmware_type'");
         $t->close;
     $l->p("Uploading file ($file)");        next;
     # use tftp to push the file up      }
     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');  
       my $host_type = $t->host_type;
     $tftp->put($file, $file)      if ($firmware_type eq 'FPGA') {
       or die "Error uploading: " . $tftp->error;        $host_type =~ s/\s.*$//;
       }
     # waitfor some sort of output  
     # make sure it says 'Success.' otherwise error      if (! exists $conf->{$firmware_type}->{$host_type}) {
     #print "LAST: " . $self->lastline;        $l->sp("No '$firmware_type' config for type $host_type");
     #my @lines = $self->getlines;        $t->close;
     #print Dump \@lines;        next;
           }
     $l->p("Checking upload ($conf->{'file_cksum'})");  
     my $results = $t->tftpd;      if (! $t->logged_in) {
     # check the 'File Length' against ???        $l->p("Logging in");
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {        $t->login($password);
       $l->sp(        unless ($t->logged_in) {
         "File checksum '" . $results->{'File Checksum'} .          $l->p('Failed!');
         "does not match config file '" . $conf->{'file_cksum'} . "'!"          $t->close;
       );          next;
       next;        }
     }      }
     $l->p("File checksum matches . . . ");  
       foreach my $k (keys %{ $conf->{general} }) {
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {        $conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k};
       $l->sp(      }
         "File length '" . $results->{'File Length'} .      $conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type;
         "does not match config file '" . $conf->{'file_size'} . " bytes'!"      $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
       );  
       next;      $l->sp("$host_type $firmware_type");
     }      ## Send commands
     $l->p("File length matches . . . ");      my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
       if ($rc) {
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {        $l->sp("Successfull!");
       $l->sp(        $host->{retry}--;
         "File name '" . $results->{'File Name'} .        $needs_reboot = 1;
         "' does not match config file '" . $conf->{'file_name'} . "'!"      } elsif (defined $rc) {
       );        $l->sp("Already up to date");
       next;        $host->{retry}--;
     }      } else {
     $l->p("File name  matches . . . ");        $l->sp("Failed");
         $t->bye;
     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");        next;
     unless ($results = $t->updateflash(      }
       args => 'mainimage ' . $ver->{'Firmware Checksum'} .  
               ' '          . $conf->{'cksum'},    }
       Timeout => 90,  
     ) ) {    if ($needs_reboot) {
       $l->sp("Couldn't update flash: $!");      $l->sp("Rebooting $host->{name}");
       next;      $t->reboot;
     }    } else {
       $l->sp("Bye $host->{name}");
     unless (      $t->bye();
       defined $results->{'Checksum'} &&    }
       $results->{'Checksum'} eq $conf->{'cksum'}  }
     ) {  
       $l->sp("Saved checksum does not match config file!");    if (! $processed) {
       next;      $l->sp("");
     }      $l->sp("Finished.  No more hosts.");
     $l->p("Uploaded checksum ($results->{'Checksum'}) " .      last;
           "matches ($conf->{'cksum'})");    }
       }
     $l->sp("Successfully updated!");  
     return 1;  sub upload
   }  {
 }    my $t    = shift;
     my $conf = shift;
 sub read_conf  
 {    my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
   my $file = shift;  
   my %conf;    my $fw_type = $conf->{firmware_type};
   my $in_ip_list = 0;  
   open my $fh, $file or die "Couldn't open file $file: $!";    my $ver = $t->ver;
   while (<$fh>) {  
     chomp;    if (! (
     next if /^#/;      $ver->{$fw_type . ' Version'}  &&
     next if /^$/;      $ver->{$fw_type . ' Checksum'}
     if ($in_ip_list) {    )) {
       s/\s+//g; # Whitespace is a no no      $l->sp("Error getting current version numbers");
       return;
       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {    }
         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);  
       } else {    if (
         push @{ $conf{'ips'} }, $_;      $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
       }      $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
     } else {    ) {
       my ($key, $val) = split /\s+/, $_, 2;      return 0;
     }
       if (lc($key) eq 'ips') {  
         $in_ip_list = 1;    $l->sp("Updating $fw_type");
         next;    $l->sp("Config information:");
       }    $l->sp("  Hardware Type: $conf->{'type'}");
     $l->sp("  File Name:     $conf->{'file_name'}");
       $key =~ s/^\s+//;    $l->sp("  File Size:     $conf->{'file_size'}");
       $key =~ s/\s+$//;    $l->sp("  File Checksum: $conf->{'file_cksum'}");
       $val =~ s/^\s+//;    $l->sp("  Conf Version:  $conf->{'ver'}");
       $val =~ s/\s+$//;    $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
     $l->sp("  Conf Checksum: $conf->{'cksum'}");
       $conf{ lc($key) } = $val;    $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
     }  
   }    my $try = 0;
   close $fh;    while (1) {
       if ($try >= $max_tries) {
   #print Dump \%conf;        $l->sp("Couldn't update in $max_tries tries!");
   foreach (        return;
     'password',      }
     'file_name', 'file_size', 'file_cksum',      $try++;
     'ver', 'cksum', 'ips',  
   ) {      $l->p("Enabling TFTPd");
     die "No $_ specified in config file!"      unless ($t->enable_tftpd) {
       if (not exists $conf{$_});        $l->sp("Couldn't enable tftpd");
   }        next;
       }
   #print Dump \%conf;  
   #exit;      $l->p("Uploading file ($conf->{file_name})");
   return \%conf;      # use tftp to push the file up
 }      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
   
 package Mylogger;      unless ($tftp->put($file, $file)) {
         $l->sp("Error uploading: " . $tftp->error);
 use Fcntl ':flock'; # import LOCK_* constants        next;
 #use YAML;      }
 use constant LOG_PRINT => 128;  
 use constant LOG_SAVE  =>  64;      $l->p("Checking upload ($conf->{'file_cksum'})");
       my $results = $t->tftpd;
 DESTROY {      # check the 'File Length' against ???
   my $self = shift;      if (! (
   if ($self->{'MYLOG'}) {        $results->{'File Checksum'} &&
     $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");        $results->{'File Length'}   &&
     close $self->{'MYLOG'};        $results->{'File Name'}
   }      )) {
 }        $l->sp("Unable to get results of upload");
         next;
 sub new {      }
   my $package = shift;      if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
   my $self = shift || {};        $l->sp(
           "File checksum (" . $results->{'File Checksum'} .
   $self->{'base_path'}  ||= '.';          ") does not match config file (" . $conf->{'file_cksum'} . ")!"
   $self->{'log_path'}   ||= $self->{'base_path'};        );
   $self->{'log_prefix'} ||= 'LOG';        next;
   $self->{'log_file'}   ||= GetLogName(      }
     $self->{'log_prefix'},      $l->p("File checksum matches . . . ");
     $self->{'log_path'}  
   );      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
   bless $self, $package;        $l->sp(
 }          "File length (" . $results->{'File Length'} .
           ") does not match config file (" . $conf->{'file_size'} . " bytes)!"
 sub s        );
 {        next;
   my $self = shift;      }
   my $m = shift;      $l->p("File length matches . . . ");
   return $self->mylog($m, LOG_SAVE);  
 }      if ( uc($results->{'File Name'}) ne uc($file) ) {
         $l->sp(
 sub p          "File name (" . $results->{'File Name'} .
 {          ") does not match config file (" . $file . ")!"
   my $self = shift;        );
   my $m = shift;        next;
   return $self->mylog($m, LOG_PRINT);      }
 }      $l->p("File name matches . . . ");
   
 sub sp      my $image_type = 'mainimage';
 {      if ($fw_type eq 'FPGA') {
   my $self = shift;        $image_type = 'fpgaimage';
   my $m = shift;      }
   return $self->mylog($m, LOG_SAVE | LOG_PRINT);      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
 }      unless ($results = $t->updateflash(
         args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
 sub mylog                ' '          . $conf->{'cksum'},
 {        Timeout => 90,
   my $self = shift;      ) ) {
         $l->sp("Couldn't update flash: $!");
   my $thing = shift;        next;
   chomp $thing;      }
   
   my $which = shift;      unless (
         defined $results->{'Checksum'} &&
   my $MYLOG;        $results->{'Checksum'} eq $conf->{'cksum'}
   if ($which & LOG_PRINT) {      ) {
     print $thing, "\n";        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
   }        next;
       }
   if ($which & LOG_SAVE) {  
     if ($self->{'MYLOG'}) {      $l->p("$fw_type saved checksum matches . . . ");
       $MYLOG = $self->{'MYLOG'};  
     } else {      return 1;
       unless ($MYLOG) {    }
                 open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})  }
           or die "Couldn't open logfile!\n";  
         my $ofh = select $MYLOG;  package Mylogger;
         $|=1;  
         select $ofh;  use Fcntl ':flock'; # import LOCK_* constants
         $self->{'MYLOG'} = $MYLOG;  #use YAML;
   use constant LOG_PRINT => 128;
         $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");  use constant LOG_SAVE  =>  64;
       }  
     }  DESTROY {
     flock($MYLOG, LOCK_EX);    my $self = shift;
     print $MYLOG (scalar gmtime), "\t", $thing, "\n"    if ($self->{'MYLOG'}) {
       or die "Couldn't print to MYLOG: $!";      $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
     flock($MYLOG, LOCK_UN);      close $self->{'MYLOG'};
   }    }
 }  }
   
 sub GetLogName  sub new {
 {    my $package = shift;
   my $prefix  = shift || die "Invalid prefix passed for log";    my $self = shift || {};
   
   my $logdate = GetLogDate();    $self->{'base_path'}  ||= '.';
   my $logver  = 0;    $self->{'log_path'}   ||= $self->{'base_path'};
   my $logname;    $self->{'log_prefix'} ||= 'LOG';
     $self->{'log_file'}   ||= GetLogName(
   do {      $self->{'log_prefix'},
     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';      $self->{'log_path'}
     $logver++;    );
   } until (not -e $logname);    bless $self, $package;
   }
   return $logname;  
 }  sub s
   {
 sub GetLogDate    my $self = shift;
 {    my $m = shift;
   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();    return $self->mylog($m, LOG_SAVE);
   }
   $mon++;  
   $year += 1900;  sub p
   {
   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_PRINT);
   if ($mday < 10) { $mday = "0$mday" }  }
   if ($mon  < 10) { $mon  = "0$mon"  }  
   sub sp
   my $time = $year . $mon . $mday;  {
     my $self = shift;
   return $time;    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.27

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