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

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

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