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

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

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