[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.15 and 1.20

version 1.15, 2005/12/30 20:26:41 version 1.20, 2007/01/31 22:19:07
Line 1 
Line 1 
 #!/usr/bin/perl  #!/usr/bin/perl
 # $RedRiver: update_trango.pl,v 1.14 2005/12/30 19:26:06 andrew Exp $  # $RedRiver: update_trango.pl,v 1.19 2007/01/31 22:14:26 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 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
   print Dump $t->sulog(args => 5);  {
   #if ( upload($t, $conf->{'file_name'}) ) {    my $t    = shift;
   #   $l->p("Rebooting");    my $file = shift;
   #  $t->reboot;  
   #} else {    my $fw_type = 'Firmware';
     $l->p("Exiting");    if (uc($conf->{'firmware_type'}) eq 'FPGA') {
     $t->exit;      $fw_type = 'FPGA';
   #}    }
     
   $l->sp("");    my $ver = $t->ver;
 }    $l->p("Current version '" . $ver->{$fw_type . ' Version'} . "'");
   
 sub upload    if (
 {      $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
   my $t    = shift;      $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
   my $file = shift;    ) {
       $l->sp("Already updated!");
   my $ver = $t->ver;      return 1;
   $l->p("Current version '$ver->{'Firmware Version'}'");    }
   
   if (    my $try = 0;
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&    while (1) {
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}      if ($try >= $max_tries) {
   ) {        $l->sp("Couldn't update in $max_tries tries!");
     $l->sp("Already updated!");        return undef;
     return 1;      }
   }      $try++;
   
   my $try = 0;      #sysinfo($self->{'_host'});
   while (1) {  
     if ($try >= $max_tries) {      $l->p("Enabling TFTPd");
       $l->sp("Couldn't update in $max_tries tries!");      $t->enable_tftpd || die "Couldn't enable tftpd";
       return undef;  
     }      $l->p("Uploading file ($file)");
     $try++;      # use tftp to push the file up
       my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
     #sysinfo($self->{'_host'});  
           $tftp->put($file, $file)
     $l->p("Enabling TFTPd");        or die "Error uploading: " . $tftp->error;
     $t->enable_tftpd || die "Couldn't enable tftpd";  
       # waitfor some sort of output
     $l->p("Uploading file ($file)");      # make sure it says 'Success.' otherwise error
     # use tftp to push the file up      #print "LAST: " . $self->lastline;
     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');      #my @lines = $self->getlines;
       #print Dump \@lines;
     $tftp->put($file, $file)  
       or die "Error uploading: " . $tftp->error;      $l->p("Checking upload ($conf->{'file_cksum'})");
       my $results = $t->tftpd;
     # waitfor some sort of output      # check the 'File Length' against ???
     # make sure it says 'Success.' otherwise error      if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
     #print "LAST: " . $self->lastline;        $l->sp(
     #my @lines = $self->getlines;          "File checksum '" . $results->{'File Checksum'} .
     #print Dump \@lines;          " does not match config file '" . $conf->{'file_cksum'} . "'!"
             );
     $l->p("Checking upload ($conf->{'file_cksum'})");        next;
     my $results = $t->tftpd;      }
     # check the 'File Length' against ???      $l->p("File checksum matches . . . ");
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {  
       $l->sp(      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
         "File checksum '" . $results->{'File Checksum'} .        $l->sp(
         "does not match config file '" . $conf->{'file_cksum'} . "'!"          "File length '" . $results->{'File Length'} .
       );          "does not match config file '" . $conf->{'file_size'} . " bytes'!"
       next;        );
     }        next;
     $l->p("File checksum matches . . . ");      }
       $l->p("File length matches . . . ");
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {  
       $l->sp(      if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
         "File length '" . $results->{'File Length'} .        $l->sp(
         "does not match config file '" . $conf->{'file_size'} . " bytes'!"          "File name '" . $results->{'File Name'} .
       );          "' does not match config file '" . $conf->{'file_name'} . "'!"
       next;        );
     }        next;
     $l->p("File length matches . . . ");      }
       $l->p("File name  matches . . . ");
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {  
       $l->sp(      my $image_type = 'mainimage';
         "File name '" . $results->{'File Name'} .      if ($fw_type eq 'FPGA') {
         "' does not match config file '" . $conf->{'file_name'} . "'!"        $image_type = 'fpgaimage';
       );      }
       next;      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
     }      unless ($results = $t->updateflash(
     $l->p("File name  matches . . . ");        args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
                 ' '          . $conf->{'cksum'},
     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");        Timeout => 90,
     unless ($results = $t->updateflash(      ) ) {
       args => 'mainimage ' . $ver->{'Firmware Checksum'} .        $l->sp("Couldn't update flash: $!");
               ' '          . $conf->{'cksum'},        next;
       Timeout => 90,      }
     ) ) {  
       $l->sp("Couldn't update flash: $!");      unless (
       next;        defined $results->{'Checksum'} &&
     }        $results->{'Checksum'} eq $conf->{'cksum'}
       ) {
     unless (        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
       defined $results->{'Checksum'} &&        next;
       $results->{'Checksum'} eq $conf->{'cksum'}      }
     ) {      $l->p("Uploaded checksum ($results->{'Checksum'}) " .
       $l->sp("Saved checksum does not match config file!");            "matches ($conf->{'cksum'})");
       next;  
     }      $l->sp("Successfully updated!");
     $l->p("Uploaded checksum ($results->{'Checksum'}) " .      return 1;
           "matches ($conf->{'cksum'})");    }
       }
     $l->sp("Successfully updated!");  
     return 1;  sub read_conf
   }  {
 }    my $file = shift;
     my %conf;
 sub read_conf    my $in_ip_list = 0;
 {    open my $fh, $file or die "Couldn't open file $file: $!";
   my $file = shift;    while (<$fh>) {
   my %conf;      chomp;
   my $in_ip_list = 0;      next if /^#/;
   open my $fh, $file or die "Couldn't open file $file: $!";      next if /^$/;
   while (<$fh>) {      if ($in_ip_list) {
     chomp;        s/\s+//g; # Whitespace is a no no
     next if /^#/;  
     next if /^$/;        if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {
     if ($in_ip_list) {          push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);
       s/\s+//g; # Whitespace is a no no        } else {
           push @{ $conf{'ips'} }, $_;
       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {        }
         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);      } else {
       } else {        my ($key, $val) = split /\s+/, $_, 2;
         push @{ $conf{'ips'} }, $_;  
       }        if (lc($key) eq 'ips') {
     } else {          $in_ip_list = 1;
       my ($key, $val) = split /\s+/, $_, 2;          next;
         }
       if (lc($key) eq 'ips') {  
         $in_ip_list = 1;        $key =~ s/^\s+//;
         next;        $key =~ s/\s+$//;
       }        $val =~ s/^\s+//;
         $val =~ s/\s+$//;
       $key =~ s/^\s+//;  
       $key =~ s/\s+$//;        $conf{ lc($key) } = $val;
       $val =~ s/^\s+//;      }
       $val =~ s/\s+$//;    }
     close $fh;
       $conf{ lc($key) } = $val;  
     }    #print Dump \%conf;
   }    foreach (
   close $fh;      'password',
       'file_name', 'file_size', 'file_cksum',
   #print Dump \%conf;      'ver', 'cksum', 'ips',
   foreach (    ) {
     'password',      die "No $_ specified in config file!"
     'file_name', 'file_size', 'file_cksum',        if (not exists $conf{$_});
     'ver', 'cksum', 'ips',    }
   ) {  
     die "No $_ specified in config file!"    #print Dump \%conf;
       if (not exists $conf{$_});    #exit;
   }    return \%conf;
   }
   #print Dump \%conf;  
   #exit;  package Mylogger;
   return \%conf;  
 }  use Fcntl ':flock'; # import LOCK_* constants
   #use YAML;
 package Mylogger;  use constant LOG_PRINT => 128;
   use constant LOG_SAVE  =>  64;
 use Fcntl ':flock'; # import LOCK_* constants  
 #use YAML;  DESTROY {
 use constant LOG_PRINT => 128;    my $self = shift;
 use constant LOG_SAVE  =>  64;    if ($self->{'MYLOG'}) {
       $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
 DESTROY {      close $self->{'MYLOG'};
   my $self = shift;    }
   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 {    $self->{'base_path'}  ||= '.';
   my $package = shift;    $self->{'log_path'}   ||= $self->{'base_path'};
   my $self = shift || {};    $self->{'log_prefix'} ||= 'LOG';
     $self->{'log_file'}   ||= GetLogName(
   $self->{'base_path'}  ||= '.';      $self->{'log_prefix'},
   $self->{'log_path'}   ||= $self->{'base_path'};      $self->{'log_path'}
   $self->{'log_prefix'} ||= 'LOG';    );
   $self->{'log_file'}   ||= GetLogName(    bless $self, $package;
     $self->{'log_prefix'},  }
     $self->{'log_path'}  
   );  sub s
   bless $self, $package;  {
 }    my $self = shift;
     my $m = shift;
 sub s    return $self->mylog($m, LOG_SAVE);
 {  }
   my $self = shift;  
   my $m = shift;  sub p
   return $self->mylog($m, LOG_SAVE);  {
 }    my $self = shift;
     my $m = shift;
 sub p    return $self->mylog($m, LOG_PRINT);
 {  }
   my $self = shift;  
   my $m = shift;  sub sp
   return $self->mylog($m, LOG_PRINT);  {
 }    my $self = shift;
     my $m = shift;
 sub sp    return $self->mylog($m, LOG_SAVE | LOG_PRINT);
 {  }
   my $self = shift;  
   my $m = shift;  sub mylog
   return $self->mylog($m, LOG_SAVE | LOG_PRINT);  {
 }    my $self = shift;
   
 sub mylog    my $thing = shift;
 {    chomp $thing;
   my $self = shift;  
     my $which = shift;
   my $thing = shift;  
   chomp $thing;    my $MYLOG;
     if ($which & LOG_PRINT) {
   my $which = shift;      print $thing, "\n";
     }
   my $MYLOG;  
   if ($which & LOG_PRINT) {    if ($which & LOG_SAVE) {
     print $thing, "\n";      if ($self->{'MYLOG'}) {
   }        $MYLOG = $self->{'MYLOG'};
       } else {
   if ($which & LOG_SAVE) {        unless ($MYLOG) {
     if ($self->{'MYLOG'}) {                  open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
       $MYLOG = $self->{'MYLOG'};            or die "Couldn't open logfile!\n";
     } else {          my $ofh = select $MYLOG;
       unless ($MYLOG) {          $|=1;
                 open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})          select $ofh;
           or die "Couldn't open logfile!\n";          $self->{'MYLOG'} = $MYLOG;
         my $ofh = select $MYLOG;  
         $|=1;          $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
         select $ofh;        }
         $self->{'MYLOG'} = $MYLOG;      }
       flock($MYLOG, LOCK_EX);
         $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");      print $MYLOG (scalar gmtime), "\t", $thing, "\n"
       }        or die "Couldn't print to MYLOG: $!";
     }      flock($MYLOG, LOCK_UN);
     flock($MYLOG, LOCK_EX);    }
     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 $logver  = 0;
   my $prefix  = shift || die "Invalid prefix passed for log";    my $logname;
   
   my $logdate = GetLogDate();    do {
   my $logver  = 0;      $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
   my $logname;      $logver++;
     } until (not -e $logname);
   do {  
     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';    return $logname;
     $logver++;  }
   } until (not -e $logname);  
   sub GetLogDate
   return $logname;  {
 }    my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
   
 sub GetLogDate    $mon++;
 {    $year += 1900;
   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();  
     if ($min  < 10) { $min  = "0$min"  }
   $mon++;    if ($sec  < 10) { $sec  = "0$sec"  }
   $year += 1900;    if ($hour < 10) { $hour = "0$hour" }
     if ($mday < 10) { $mday = "0$mday" }
   if ($min  < 10) { $min  = "0$min"  }    if ($mon  < 10) { $mon  = "0$mon"  }
   if ($sec  < 10) { $sec  = "0$sec"  }  
   if ($hour < 10) { $hour = "0$hour" }    my $time = $year . $mon . $mday;
   if ($mday < 10) { $mday = "0$mday" }  
   if ($mon  < 10) { $mon  = "0$mon"  }    return $time;
   }
   my $time = $year . $mon . $mday;  
   
   return $time;  
 }  

Legend:
Removed from v.1.15  
changed lines
  Added in v.1.20

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