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

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

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