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

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

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