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

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

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