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

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

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