[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.2 and 1.22

version 1.2, 2005/11/16 05:26:37 version 1.22, 2007/02/01 17:58:33
Line 1 
Line 1 
 #!/usr/bin/perl  #!/usr/bin/perl
 # $RedRiver: update_trango.pl,v 1.1 2005/11/16 03:28:42 andrew Exp $  # $RedRiver: update_trango.pl,v 1.21 2007/02/01 17:08:44 mike 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::Telnet;  use YAML qw/ Dump LoadFile /;
 use Net::TFTP;  use Net::TFTP;
 use YAML;  use lib '.';
   use Net::Telnet::Trango;
 my @Foxes = (  
   '10.100.3.8',  my $config_file = shift || 'update_trango.yaml';
 #  '10.100.3.11',  my $max_tries = 3;
 #  '10.100.3.12',  
 #  '10.100.3.13',  
 );  my $l = Mylogger->new( { log_prefix => 'UT' } );
   
 my $password = 'password';  
   $l->sp("Reading config file '$config_file'");
 my $new_file  = 'fsu53_2p0a2H0003D05101403.s19';  my $conf = LoadFile($config_file);
 my $new_ver   = 'FSU 1p06H0003D04111001';  
 my $new_cksum = 'x1D28DCC4';  #$l->sp("  Hardware Type: $conf->{'type'}");
   #$l->sp("  File Name:     $conf->{'file_name'}");
 my $max_tries = 3;  #$l->sp("  File Size:     $conf->{'file_size'}");
   #$l->sp("  File Checksum: $conf->{'file_cksum'}");
   #$l->sp("  FW Version:    $conf->{'ver'}");
   #$l->sp("  FW Checksum:   $conf->{'cksum'}");
   #$l->sp("");
   
   my @foxes = (
 foreach my $fox (@Foxes) {  '10.100.1.2',
   print "getting temp from Fox: $fox\n";  '10.100.2.2',
   ## Connect and login.  '10.100.3.2',
   my $host = new Net::Telnet (Timeout => 5,  '10.100.4.2',
                               Prompt => '/#> *$/');  );
   $host->open($fox);  
   $host->dump_log('dump.log');  foreach my $fox (@foxes) {
     $l->sp("Checking: $fox");
   ## Login to remote host.    my $needs_reboot = 0;
   $host->waitfor(  
     -match => '/password: ?$/i',    ## Connect and login.
     -errmode => "return",    my $t = new Net::Telnet::Trango (
   ) or die "problem connecting to host ($fox): ", $host->lastline;      Timeout => 5,
   $host->print($password);      Errmode => 'return',
   $host->waitfor(    ) or die "Couldn't make new connection: $!";
     -match => $host->prompt,    $l->p("Connecting to $fox");
     -errmode => "return",    unless ( $t->open($fox) ) {
   ) or die "login ($fox) failed: ", $host->lastline;      $l->sp("Error connecting: $!");
         next;
     }
   ## Send commands  
   if ( upload($host, $new_file) ) {    foreach my $firmware_type ('Firmware', 'FPGA') {
     $host->send('reboot');  
   } else {      if (! exists $conf->{$firmware_type}) {
     $host->send('exit');            $l->s("No configs for '$firmware_type'");
   }            $t->close();
   $host->close;            next;
 }          }
   
 sub upload      my $host_type = $t->host_type;
 {      if (uc($conf->{'firmware_type'}) eq 'FPGA') {
   my $host = shift;        $host_type =~ s/\s.*$//;
   my $file = shift;      }
   my $ver = get_ver($host);  
       if (! exists $conf->{$firmware_type}->{$host_type}) {
   if (            $l->sp("No '$firmware_type' config for type $host_type");
     $ver->{'Firmware Version'} eq $new_ver &&            $t->close();
     $ver->{'Checksum'}         eq $new_cksum            next;
   ) {          }
     print "Already updated!";  
     return 1;      unless ($t->logged_in) {
   }        $l->p("Logging in");
         $t->login($conf->{general}->{'password'}) || die "Couldn't login: $!";
   my $try = 0;            $t->exit();
   while (1) {      }
     $try++;  
           foreach my $k (keys %{ $conf->{general} }) {
     enable_tftpd($host) || die "Couldn't enable tftpd";            $conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k};
       }
     # use tftp to push the file up          $conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type;
           $conf->{$firmware_type}->{$host_type}->{type} ||= $host_type;
     # waitfor some sort of output  
     # make sure it says 'Success.' otherwise error      $l->sp("Updating: $host_type $firmware_type on $fox");
           $l->p("Sending commands");
     my $results = check_tftpd($host);      ## Send commands
     # check the 'File Length' against ???      if ( upload($t, $conf->{$firmware_type}->{$host_type}) ) {
         $l->sp("Successfull!");
     # 'updateflash mainimage $old_cksum $new_cksum'            $needs_reboot = 1;
     # OR      } else {
     # 'save mainimage [current firmware checksum] [new firmware checksum]'            $l->sp("Failed");
     # waitfor a prompt, look at the end of the output for 'Success.' otherwise      }
     # error.  
     # decode_lines to get 'Checksum' to see if it matches $new_cksum      $l->sp("");
         }
     $ver = get_ver($host);  
     # check versions    if ($needs_reboot) {
           $l->sp("Rebooting $fox");
       $t->reboot;
     if ($try >= $max_tries) {    } else {
       warn "Couldn't update in $max_tries tries!";      $l->sp("Exiting $fox");
       return undef;      $t->exit();
     }    }
   }    exit;
 }  }
   
 sub get_ver  sub upload
 {  {
   my $host = shift;    my $t    = shift;
   return cmd($host, 'ver');    my $conf = shift;
 }  
     my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
 sub enable_tftpd  
 {    my $fw_type = $conf->{firmware_type};
   my $host = shift;  
     my $ver = $t->ver;
   my $vals = cmd($host, 'tftpd on');    print Dump $ver;
     $l->p("Current version '" . $ver->{$fw_type . ' Version'} . "'");
   if ($vals->{'Tftpd'} eq 'listen') {    exit;
     return 1;  
   } else {    if (
     return undef;      $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
   }      $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
 }    ) {
       $l->sp("Already updated!");
 sub check_tftpd      return 0;
 {    }
   my $host = shift;  
   return cmd($host, 'tftpd');    my $try = 0;
 }    while (1) {
       if ($try >= $max_tries) {
 sub cmd        $l->sp("Couldn't update in $max_tries tries!");
 {        return undef;
   my $host   = shift;      }
   my $string = shift;      $try++;
   
   my @lines = $host->cmd($string);      #sysinfo($self->{'_host'});
   
   my $vals = decode_lines(@lines);      $l->p("Enabling TFTPd");
   return $vals;      unless ($t->enable_tftpd) {
 }        $l->sp("Couldn't enable tftpd");
         next;
 sub decode_lines      }
 {  
   ### XXX ver has 2 Checksums.  one for FPGA and one for Firmware.  DOH!      $l->p("Uploading file ($file)");
   my @lines = @_;      # use tftp to push the file up
       my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
   my %conf;  
       unless ($tftp->put($file, $file)) {
   my $key = '';        $l->sp("Error uploading: " . $tftp->error);
   my $val = '';        next;
   my $in_key = 0;      }
   my $in_val = 0;  
       $l->p("Checking upload ($conf->{'file_cksum'})");
   foreach my $line (@lines) {      my $results = $t->tftpd;
     my @chars = split //, $line;      # check the 'File Length' against ???
       if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
     foreach my $c (@chars) {        $l->sp(
       next if $c eq "\r";          "File checksum '" . $results->{'File Checksum'} .
       next if $c eq "\n";          " does not match config file '" . $conf->{'file_cksum'} . "'!"
         );
       if ($c eq '[') {        next;
         $in_key = 1;      }
         $in_val = 0;      $l->p("File checksum matches . . . ");
         if ($key) {  
           $val =~ s/\s+$//;      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
           $conf{$key} = $val;        $l->sp(
           $key = '';          "File length '" . $results->{'File Length'} .
           $val = '';          "does not match config file '" . $conf->{'file_size'} . " bytes'!"
         }        );
         next;
       } elsif ($c eq ']') {      }
         $in_val = 1;      $l->p("File length matches . . . ");
         $in_key = 0;  
         $c = shift @chars;      if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
         $l->sp(
       } elsif ($in_key) {          "File name '" . $results->{'File Name'} .
         $key .= $c;          "' does not match config file '" . $conf->{'file_name'} . "'!"
         );
       } elsif ($in_val) {        next;
         $val .= $c;      }
       }      $l->p("File name  matches . . . ");
     }  
   }      my $image_type = 'mainimage';
   print Dump \%conf;      if ($fw_type eq 'FPGA') {
         $image_type = 'fpgaimage';
   if (%conf) {      }
     return \%conf;      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
   } else {      unless ($results = $t->updateflash(
     return \@lines;        args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
   }                ' '          . $conf->{'cksum'},
 }        Timeout => 90,
       ) ) {
         $l->sp("Couldn't update flash: $!");
         next;
       }
   
       unless (
         defined $results->{'Checksum'} &&
         $results->{'Checksum'} eq $conf->{'cksum'}
       ) {
         $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
         next;
       }
       $l->p("Uploaded checksum ($results->{'Checksum'}) " .
             "matches ($conf->{'cksum'})");
   
       $l->sp("Successfully updated!");
       return 1;
     }
   }
   
   package Mylogger;
   
   use Fcntl ':flock'; # import LOCK_* constants
   #use YAML;
   use constant LOG_PRINT => 128;
   use constant LOG_SAVE  =>  64;
   
   DESTROY {
     my $self = shift;
     if ($self->{'MYLOG'}) {
       $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
       close $self->{'MYLOG'};
     }
   }
   
   sub new {
     my $package = shift;
     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.2  
changed lines
  Added in v.1.22

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