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

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

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