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

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

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.24

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