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

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

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