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

version 1.12, 2005/12/29 18:41:17 version 1.24, 2007/02/02 16:29:58
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.23 2007/02/01 18:44:30 andrew Exp $
 ########################################################################  ########################################################################
 # update_trango.pl *** Updates trango foxes with a new firmware  # update_trango.pl *** Updates trango foxes 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;
   
 my $config_file = shift || 'update_trango.conf';  my $config_file = shift || 'update_trango.yaml';
 my $max_tries = 3;  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 @foxes = (
 my $conf = read_conf($config_file);  '10.100.1.2',
   '10.100.2.2',
 $l->sp("  Hardware Type: $conf->{'type'}");  '10.100.3.2',
 $l->sp("  File Name:     $conf->{'file_name'}");  '10.100.3.3',
 $l->sp("  File Size:     $conf->{'file_size'}");  '10.100.3.4',
 $l->sp("  File Checksum: $conf->{'file_cksum'}");  '10.100.3.8',
 $l->sp("  FW Version:    $conf->{'ver'}");  '10.100.4.2',
 $l->sp("  FW Checksum:   $conf->{'cksum'}");  '10.100.60.5',
 $l->sp("");  '10.100.80.5',
   '10.100.150.5',
   '10.100.5.2',
   '10.100.6.2',
   '10.100.7.2',
   '10.100.8.2',
   '10.100.9.2',
 foreach my $fox (@{ $conf->{'ips'} }) {  '10.100.21.2',
   $l->sp("Updating: $fox");  '10.100.22.2',
   '10.100.23.2',
   ## Connect and login.  '10.100.32.2',
   my $t = new Net::Telnet::Trango ({  '10.100.33.2',
     Host    => $fox,  '10.100.41.2',
     Timeout => 5,  '10.100.51.2',
   });  '10.100.52.2',
   '10.100.53.2',
   $l->p("Connecting to $fox");  '10.100.54.2',
   my ($type, $version) = $t->connect();  '10.100.61.2',
   '10.100.62.2',
   unless (defined $type && defined $version) {  );
     $l->sp("Error connecting!");  
     next;  foreach my $fox (@foxes) {
   }    $l->sp("Checking: $fox");
     my $needs_reboot = 0;
   if ($type ne $conf->{'type'}) {  
     $l->sp("Wrong type of unit ('$type' should be '$conf->{'type'}')");    ## Connect and login.
     $t->close;    my $t = new Net::Telnet::Trango (
     next;      Timeout => 5,
   }      Errmode => 'return',
     ) or die "Couldn't make new connection: $!";
   if ($version eq $conf->{'ver'}) {    $l->p("Connecting to $fox");
     $l->sp("Already up to date with firmware version '$version'");    unless ( $t->open($fox) ) {
     $t->close;      $l->sp("Error connecting: $!");
     next;      next;
   }    }
   
   $l->p("Logging in");    foreach my $firmware_type ('Firmware', 'FPGA') {
   $t->login($conf->{'password'}) || die "Couldn't login: $!";  
       if (! exists $conf->{$firmware_type}) {
   $l->p("Sending commands");            $l->s("No configs for '$firmware_type'");
   ## Send commands            $t->close();
   #print Dump $t->ver();            next;
   #print Dump $t->tftpd();          }
   if ( upload($t, $conf->{'file_name'}) ) {  
     $l->p("Rebooting");      my $host_type = $t->host_type;
     $t->reboot;      if ($firmware_type eq 'FPGA') {
   } else {        $host_type =~ s/\s.*$//;
     $l->p("Exiting");      }
     $t->exit;  
   }      if (! exists $conf->{$firmware_type}->{$host_type}) {
               $l->sp("No '$firmware_type' config for type $host_type");
   $t->close;            $t->close();
   $l->sp("");            next;
 }          }
   
 sub upload      if (! $t->logged_in) {
 {        $l->p("Logging in");
   my $t    = shift;        $t->login($conf->{general}->{'password'});
   my $file = shift;        unless ($t->logged_in) {
           $l->p('Failed!');
   my $ver = $t->ver;          $t->bye();
   $l->p("Current version '$ver->{'Firmware Version'}'");          next;
         }
   if (      }
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&  
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}          foreach my $k (keys %{ $conf->{general} }) {
   ) {            $conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k};
     $l->sp("Already updated!");      }
     return 1;          $conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type;
   }          $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
   
   my $try = 0;      $l->sp("$host_type $firmware_type");
   while (1) {      $l->p("Sending commands");
     if ($try >= $max_tries) {      ## Send commands
       $l->sp("Couldn't update in $max_tries tries!");      my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
       return undef;          if ($rc) {
     }        $l->sp("Successfull!");
     $try++;            $needs_reboot = 1;
       } elsif (defined $rc) {
     #sysinfo($self->{'_host'});            $l->sp("Already up to date");
           } else {
     $l->p("Enabling TFTPd");            $l->sp("Failed");
     $t->enable_tftpd || die "Couldn't enable tftpd";            $t->bye;
             next;
     $l->p("Uploading file ($file)");      }
     # use tftp to push the file up  
     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');    }
   
     $tftp->put($file, $file)    if ($needs_reboot) {
       or die "Error uploading: " . $tftp->error;      $l->sp("Rebooting $fox");
       $t->reboot;
     # waitfor some sort of output    } else {
     # make sure it says 'Success.' otherwise error      $l->sp("Bye $fox");
     #print "LAST: " . $self->lastline;      $t->bye();
     #my @lines = $self->getlines;    }
     #print Dump \@lines;    $l->sp("");
       }
     $l->p("Checking upload ($conf->{'file_cksum'})");  
     my $results = $t->tftpd;  sub upload
     # check the 'File Length' against ???  {
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {    my $t    = shift;
       $l->sp(    my $conf = shift;
         "File checksum '" . $results->{'File Checksum'} .  
         "does not match config file '" . $conf->{'file_cksum'} . "'!"    my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
       );  
       next;    my $fw_type = $conf->{firmware_type};
     }  
     $l->p("File checksum matches . . . ");    my $ver = $t->ver;
   
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {    if (
       $l->sp(      $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
         "File length '" . $results->{'File Length'} .      $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
         "does not match config file '" . $conf->{'file_size'} . " bytes'!"    ) {
       );      return 0;
       next;    }
     }  
     $l->p("File length matches . . . ");    $l->sp("Config information:");
     $l->sp("  Hardware Type: $conf->{'type'}");
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {    $l->sp("  File Name:     $conf->{'file_name'}");
       $l->sp(    $l->sp("  File Size:     $conf->{'file_size'}");
         "File name '" . $results->{'File Name'} .    $l->sp("  File Checksum: $conf->{'file_cksum'}");
         "' does not match config file '" . $conf->{'file_name'} . "'!"    $l->sp("  Conf Version:  $conf->{'ver'}");
       );    $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
       next;    $l->sp("  Conf Checksum: $conf->{'cksum'}");
     }    $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
     $l->p("File name  matches . . . ");  
     $l->sp("Updating");
     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");    my $try = 0;
     unless ($results = $t->updateflash(    while (1) {
       $ver->{'Firmware Checksum'}, $conf->{'cksum'}      if ($try >= $max_tries) {
     ) ) {        $l->sp("Couldn't update in $max_tries tries!");
       $l->sp("Couldn't update flash: $!");        return;
       next;      }
     }      $try++;
   
     unless (      #sysinfo($self->{'_host'});
       defined $results->{'Checksum'} &&  
       $results->{'Checksum'} eq $conf->{'cksum'}      $l->p("Enabling TFTPd");
     ) {      unless ($t->enable_tftpd) {
       $l->sp("Saved checksum does not match config file!");        $l->sp("Couldn't enable tftpd");
       next;        next;
     }      }
     $l->p("Uploaded checksum ($results->{'Checksum'}) " .  
           "matches ($conf->{'cksum'})");      $l->p("Uploading file ($file)");
           # use tftp to push the file up
     $l->sp("Successfully updated!");      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
     return 1;  
   }      unless ($tftp->put($file, $file)) {
 }        $l->sp("Error uploading: " . $tftp->error);
         next;
 sub read_conf      }
 {  
   my $file = shift;      $l->p("Checking upload ($conf->{'file_cksum'})");
   my %conf;      my $results = $t->tftpd;
   my $in_ip_list = 0;      # check the 'File Length' against ???
   open my $fh, $file or die "Couldn't open file $file: $!";      if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
   while (<$fh>) {        $l->sp(
     chomp;          "File checksum '" . $results->{'File Checksum'} .
     next if /^#/;          " does not match config file '" . $conf->{'file_cksum'} . "'!"
     next if /^$/;        );
     if ($in_ip_list) {        next;
       s/\s+//g; # Whitespace is a no no      }
       $l->p("File checksum matches . . . ");
       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {  
         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
       } else {        $l->sp(
         push @{ $conf{'ips'} }, $_;          "File length '" . $results->{'File Length'} .
       }          "does not match config file '" . $conf->{'file_size'} . " bytes'!"
     } else {        );
       my ($key, $val) = split /\s+/, $_, 2;        next;
       }
       if (lc($key) eq 'ips') {      $l->p("File length matches . . . ");
         $in_ip_list = 1;  
         next;      if ( uc($results->{'File Name'}) ne uc($file) ) {
       }        $l->sp(
           "File name '" . $results->{'File Name'} .
       $key =~ s/^\s+//;          "' does not match config file '" . $file . "'!"
       $key =~ s/\s+$//;        );
       $val =~ s/^\s+//;        next;
       $val =~ s/\s+$//;      }
       $l->p("File name  matches . . . ");
       $conf{ lc($key) } = $val;  
     }      my $image_type = 'mainimage';
   }      if ($fw_type eq 'FPGA') {
   close $fh;        $image_type = 'fpgaimage';
       }
   #print Dump \%conf;      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
   foreach (      unless ($results = $t->updateflash(
     'password',        args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
     'file_name', 'file_size', 'file_cksum',                ' '          . $conf->{'cksum'},
     'ver', 'cksum', 'ips',        Timeout => 90,
   ) {      ) ) {
     die "No $_ specified in config file!"        $l->sp("Couldn't update flash: $!");
       if (not exists $conf{$_});        next;
   }      }
   
   #print Dump \%conf;      unless (
   #exit;        defined $results->{'Checksum'} &&
   return \%conf;        $results->{'Checksum'} eq $conf->{'cksum'}
 }      ) {
         $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
 package Mylogger;        next;
       }
 use Fcntl ':flock'; # import LOCK_* constants      $l->p("Uploaded checksum ($results->{'Checksum'}) " .
 #use YAML;            "matches ($conf->{'cksum'})");
 use constant LOG_PRINT => 128;  
 use constant LOG_SAVE  =>  64;      $l->sp("Successfully updated!");
       return 1;
 DESTROY {    }
   my $self = shift;  }
   if ($self->{'MYLOG'}) {  
     $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");  package Mylogger;
     close $self->{'MYLOG'};  
   }  use Fcntl ':flock'; # import LOCK_* constants
 }  #use YAML;
   use constant LOG_PRINT => 128;
 sub new {  use constant LOG_SAVE  =>  64;
   my $package = shift;  
   my $self = shift || {};  DESTROY {
     my $self = shift;
   $self->{'base_path'}  ||= '.';    if ($self->{'MYLOG'}) {
   $self->{'log_path'}   ||= $self->{'base_path'};      $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
   $self->{'log_prefix'} ||= 'LOG';      close $self->{'MYLOG'};
   $self->{'log_file'}   ||= GetLogName(    }
     $self->{'log_prefix'},  }
     $self->{'log_path'}  
   );  sub new {
   bless $self, $package;    my $package = shift;
 }    my $self = shift || {};
   
 sub s    $self->{'base_path'}  ||= '.';
 {    $self->{'log_path'}   ||= $self->{'base_path'};
   my $self = shift;    $self->{'log_prefix'} ||= 'LOG';
   my $m = shift;    $self->{'log_file'}   ||= GetLogName(
   return $self->mylog($m, LOG_SAVE);      $self->{'log_prefix'},
 }      $self->{'log_path'}
     );
 sub p    bless $self, $package;
 {  }
   my $self = shift;  
   my $m = shift;  sub s
   return $self->mylog($m, LOG_PRINT);  {
 }    my $self = shift;
     my $m = shift;
 sub sp    return $self->mylog($m, LOG_SAVE);
 {  }
   my $self = shift;  
   my $m = shift;  sub p
   return $self->mylog($m, LOG_SAVE | LOG_PRINT);  {
 }    my $self = shift;
     my $m = shift;
 sub mylog    return $self->mylog($m, LOG_PRINT);
 {  }
   my $self = shift;  
   sub sp
   my $thing = shift;  {
   chomp $thing;    my $self = shift;
     my $m = shift;
   my $which = shift;    return $self->mylog($m, LOG_SAVE | LOG_PRINT);
   }
   my $MYLOG;  
   if ($which & LOG_PRINT) {  sub mylog
     print $thing, "\n";  {
   }    my $self = shift;
   
   if ($which & LOG_SAVE) {    my $thing = shift;
     if ($self->{'MYLOG'}) {    chomp $thing;
       $MYLOG = $self->{'MYLOG'};  
     } else {    my $which = shift;
       unless ($MYLOG) {  
                 open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})    my $MYLOG;
           or die "Couldn't open logfile!\n";    if ($which & LOG_PRINT) {
         my $ofh = select $MYLOG;      print $thing, "\n";
         $|=1;    }
         select $ofh;  
         $self->{'MYLOG'} = $MYLOG;    if ($which & LOG_SAVE) {
       if ($self->{'MYLOG'}) {
         $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");        $MYLOG = $self->{'MYLOG'};
       }      } else {
     }        unless ($MYLOG) {
     flock($MYLOG, LOCK_EX);                  open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
     print $MYLOG (scalar gmtime), "\t", $thing, "\n"            or die "Couldn't open logfile!\n";
       or die "Couldn't print to MYLOG: $!";          my $ofh = select $MYLOG;
     flock($MYLOG, LOCK_UN);          $|=1;
   }          select $ofh;
 }          $self->{'MYLOG'} = $MYLOG;
   
 sub GetLogName          $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
 {        }
   my $prefix  = shift || die "Invalid prefix passed for log";      }
       flock($MYLOG, LOCK_EX);
   my $logdate = GetLogDate();      print $MYLOG (scalar gmtime), "\t", $thing, "\n"
   my $logver  = 0;        or die "Couldn't print to MYLOG: $!";
   my $logname;      flock($MYLOG, LOCK_UN);
     }
   do {  }
     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';  
     $logver++;  sub GetLogName
   } until (not -e $logname);  {
     my $prefix  = shift || die "Invalid prefix passed for log";
   return $logname;  
 }    my $logdate = GetLogDate();
     my $logver  = 0;
 sub GetLogDate    my $logname;
 {  
   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();    do {
       $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
   $mon++;      $logver++;
   $year += 1900;    } until (not -e $logname);
   
   if ($min  < 10) { $min  = "0$min"  }    return $logname;
   if ($sec  < 10) { $sec  = "0$sec"  }  }
   if ($hour < 10) { $hour = "0$hour" }  
   if ($mday < 10) { $mday = "0$mday" }  sub GetLogDate
   if ($mon  < 10) { $mon  = "0$mon"  }  {
     my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
   my $time = $year . $mon . $mday;  
     $mon++;
   return $time;    $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.24

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