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

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

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