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

version 1.14, 2005/12/30 19:26:06 version 1.19, 2007/01/31 22:14:26
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.18 2007/01/31 19:01:12 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 Net::TFTP;
 use YAML;  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.conf';  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 = read_conf($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("");  
   
   
   
   
   
   foreach my $fox (@{ $conf->{'ips'} }) {
 foreach my $fox (@{ $conf->{'ips'} }) {    $l->sp("Updating: $fox");
   $l->sp("Updating: $fox");  
     ## Connect and login.
   ## Connect and login.    my $t = new Net::Telnet::Trango (
   my $t = new Net::Telnet::Trango (      Timeout => 5,
     Timeout => 5,      Errmode => 'return',
     Errmode => 'return',    ) or die "Couldn't make new connection: $!";
   ) or die "Couldn't make new connection: $!";    $l->p("Connecting to $fox");
     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'}')");
   if ($t->host_type ne $conf->{'type'}) {      $t->close;
     $l->sp("Wrong type of unit ('$t->host_type' should be '$conf->{'type'}')");      next;
     $t->close;    }
     next;  
   }    if ($t->firmware_version eq $conf->{'ver'}) {
       $l->sp("Already up to date with firmware version '" . $t->firmware_version . "'");
   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;  
   }    $l->p("Logging in");
     $t->login($conf->{'password'}) || die "Couldn't login: $!";
   $l->p("Logging in");  
   $t->login($conf->{'password'}) || die "Couldn't login: $!";    $l->p("Sending commands");
     ## Send commands
   $l->p("Sending commands");    if ( upload($t, $conf->{'file_name'}) ) {
   ## Send commands       $l->p("Rebooting");
   #print Dump $t->login_banner;      $t->reboot;
   #print Dump $t->sysinfo;    } else {
   #print Dump $t->last_lines;      $l->p("Exiting");
   #print Dump $t->ver();      $t->exit;
   print Dump $t->tftpd();    }
   print Dump $t->enable_tftpd();  
   #print Dump [ "Exit", $t->exit ];    $l->sp("");
   print Dump $t->tftpd();  }
   print Dump $t->disable_tftpd();  
   print Dump $t->tftpd();  sub upload
   #if ( upload($t, $conf->{'file_name'}) ) {  {
   #   $l->p("Rebooting");    my $t    = shift;
   #  $t->reboot;    my $file = shift;
   #} else {  
     $l->p("Exiting");    my $fw_type = 'Firmware';
     $t->exit;    if (uc($conf->{'firmware_type'}) eq 'FPGA') {
   #}      $fw_type = 'FPGA';
       }
   $l->sp("");  
 }    my $ver = $t->ver;
     $l->p("Current version '" . $ver->{$fw_type . ' Version'} . "'");
 sub upload  
 {    if (
   my $t    = shift;      $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
   my $file = shift;      $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
     ) {
   my $ver = $t->ver;      $l->sp("Already updated!");
   $l->p("Current version '$ver->{'Firmware Version'}'");      return 1;
     }
   if (  
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&    my $try = 0;
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}    while (1) {
   ) {      if ($try >= $max_tries) {
     $l->sp("Already updated!");        $l->sp("Couldn't update in $max_tries tries!");
     return 1;        return undef;
   }      }
       $try++;
   my $try = 0;  
   while (1) {      #sysinfo($self->{'_host'});
     if ($try >= $max_tries) {  
       $l->sp("Couldn't update in $max_tries tries!");      $l->p("Enabling TFTPd");
       return undef;      $t->enable_tftpd || die "Couldn't enable tftpd";
     }  
     $try++;      $l->p("Uploading file ($file)");
       # use tftp to push the file up
     #sysinfo($self->{'_host'});      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
       
     $l->p("Enabling TFTPd");      $tftp->put($file, $file)
     $t->enable_tftpd || die "Couldn't enable tftpd";        or die "Error uploading: " . $tftp->error;
   
     $l->p("Uploading file ($file)");      # waitfor some sort of output
     # use tftp to push the file up      # make sure it says 'Success.' otherwise error
     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');      #print "LAST: " . $self->lastline;
       #my @lines = $self->getlines;
     $tftp->put($file, $file)      #print Dump \@lines;
       or die "Error uploading: " . $tftp->error;  
       $l->p("Checking upload ($conf->{'file_cksum'})");
     # waitfor some sort of output      my $results = $t->tftpd;
     # make sure it says 'Success.' otherwise error      # check the 'File Length' against ???
     #print "LAST: " . $self->lastline;      if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
     #my @lines = $self->getlines;        $l->sp(
     #print Dump \@lines;          "File checksum '" . $results->{'File Checksum'} .
               "does not match config file '" . $conf->{'file_cksum'} . "'!"
     $l->p("Checking upload ($conf->{'file_cksum'})");        );
     my $results = $t->tftpd;        next;
     # check the 'File Length' against ???      }
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {      $l->p("File checksum matches . . . ");
       $l->sp(  
         "File checksum '" . $results->{'File Checksum'} .      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
         "does not match config file '" . $conf->{'file_cksum'} . "'!"        $l->sp(
       );          "File length '" . $results->{'File Length'} .
       next;          "does not match config file '" . $conf->{'file_size'} . " bytes'!"
     }        );
     $l->p("File checksum matches . . . ");        next;
       }
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {      $l->p("File length matches . . . ");
       $l->sp(  
         "File length '" . $results->{'File Length'} .      if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
         "does not match config file '" . $conf->{'file_size'} . " bytes'!"        $l->sp(
       );          "File name '" . $results->{'File Name'} .
       next;          "' does not match config file '" . $conf->{'file_name'} . "'!"
     }        );
     $l->p("File length matches . . . ");        next;
       }
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {      $l->p("File name  matches . . . ");
       $l->sp(  
         "File name '" . $results->{'File Name'} .      my $image_type = 'mainimage';
         "' does not match config file '" . $conf->{'file_name'} . "'!"      if ($fw_type eq 'FPGA') {
       );        $image_type = 'fpgaimage';
       next;      }
     }      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
     $l->p("File name  matches . . . ");      unless ($results = $t->updateflash(
         args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");                ' '          . $conf->{'cksum'},
     unless ($results = $t->updateflash(        Timeout => 90,
       args => 'mainimage ' . $ver->{'Firmware Checksum'} .      ) ) {
               ' '          . $conf->{'cksum'},        $l->sp("Couldn't update flash: $!");
       Timeout => 90,        next;
     ) ) {      }
       $l->sp("Couldn't update flash: $!");  
       next;      unless (
     }        defined $results->{'Checksum'} &&
         $results->{'Checksum'} eq $conf->{'cksum'}
     unless (      ) {
       defined $results->{'Checksum'} &&        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
       $results->{'Checksum'} eq $conf->{'cksum'}        next;
     ) {      }
       $l->sp("Saved checksum does not match config file!");      $l->p("Uploaded checksum ($results->{'Checksum'}) " .
       next;            "matches ($conf->{'cksum'})");
     }  
     $l->p("Uploaded checksum ($results->{'Checksum'}) " .      $l->sp("Successfully updated!");
           "matches ($conf->{'cksum'})");      return 1;
         }
     $l->sp("Successfully updated!");  }
     return 1;  
   }  sub read_conf
 }  {
     my $file = shift;
 sub read_conf    my %conf;
 {    my $in_ip_list = 0;
   my $file = shift;    open my $fh, $file or die "Couldn't open file $file: $!";
   my %conf;    while (<$fh>) {
   my $in_ip_list = 0;      chomp;
   open my $fh, $file or die "Couldn't open file $file: $!";      next if /^#/;
   while (<$fh>) {      next if /^$/;
     chomp;      if ($in_ip_list) {
     next if /^#/;        s/\s+//g; # Whitespace is a no no
     next if /^$/;  
     if ($in_ip_list) {        if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {
       s/\s+//g; # Whitespace is a no no          push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);
         } else {
       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {          push @{ $conf{'ips'} }, $_;
         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);        }
       } else {      } else {
         push @{ $conf{'ips'} }, $_;        my ($key, $val) = split /\s+/, $_, 2;
       }  
     } else {        if (lc($key) eq 'ips') {
       my ($key, $val) = split /\s+/, $_, 2;          $in_ip_list = 1;
           next;
       if (lc($key) eq 'ips') {        }
         $in_ip_list = 1;  
         next;        $key =~ s/^\s+//;
       }        $key =~ s/\s+$//;
         $val =~ s/^\s+//;
       $key =~ s/^\s+//;        $val =~ s/\s+$//;
       $key =~ s/\s+$//;  
       $val =~ s/^\s+//;        $conf{ lc($key) } = $val;
       $val =~ s/\s+$//;      }
     }
       $conf{ lc($key) } = $val;    close $fh;
     }  
   }    #print Dump \%conf;
   close $fh;    foreach (
       'password',
   #print Dump \%conf;      'file_name', 'file_size', 'file_cksum',
   foreach (      'ver', 'cksum', 'ips',
     'password',    ) {
     'file_name', 'file_size', 'file_cksum',      die "No $_ specified in config file!"
     'ver', 'cksum', 'ips',        if (not exists $conf{$_});
   ) {    }
     die "No $_ specified in config file!"  
       if (not exists $conf{$_});    #print Dump \%conf;
   }    #exit;
     return \%conf;
   #print Dump \%conf;  }
   #exit;  
   return \%conf;  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.14  
changed lines
  Added in v.1.19

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