[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.15 and 1.24

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

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

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