[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.14 and 1.30

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

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