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

version 1.14, 2005/12/30 19:26:06 version 1.26, 2007/02/02 18:08:56
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.25 2007/02/02 17:50:09 andrew Exp $
 ########################################################################  ########################################################################
 # update_trango.pl *** Updates trango foxes with a new firmware  # update_trango.pl *** Updates trango hosts 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 YAML qw/ LoadFile /;
 use YAML;  use Net::TFTP;
 use lib '.';  use lib '.';
 use Net::Telnet::Trango;  use Net::Telnet::Trango;
   use RedRiver::Wireless;
 my $config_file = shift || 'update_trango.conf';  
 my $max_tries = 3;  my $config_file = shift || 'update_trango.yaml';
   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'");
   my $conf = LoadFile($config_file);
 $l->sp("Reading config file '$config_file'");  
 my $conf = read_conf($config_file);  $l->sp("Reading hosts");
   my $hosts = RedRiver::Wireless::Read_Hosts();
 $l->sp("  Hardware Type: $conf->{'type'}");  
 $l->sp("  File Name:     $conf->{'file_name'}");  
 $l->sp("  File Size:     $conf->{'file_size'}");  
 $l->sp("  File Checksum: $conf->{'file_cksum'}");  foreach my $host (@{ $hosts }) {
 $l->sp("  FW Version:    $conf->{'ver'}");    next if $host->{group} !~ /^Trango/;
 $l->sp("  FW Checksum:   $conf->{'cksum'}");    $l->sp("");
 $l->sp("");    $l->sp("Checking: $host->{name}");
     my $needs_reboot = 0;
   
     ## Connect and login.
     my $t = new Net::Telnet::Trango (
       Timeout => 5,
       Errmode => 'return',
 foreach my $fox (@{ $conf->{'ips'} }) {    ) or die "Couldn't make new connection: $!";
   $l->sp("Updating: $fox");    $l->p("Connecting to $host->{name}");
     unless ( $t->open($host->{name}) ) {
   ## Connect and login.      $l->sp("Error connecting: $!");
   my $t = new Net::Telnet::Trango (      next;
     Timeout => 5,    }
     Errmode => 'return',  
   ) or die "Couldn't make new connection: $!";    my $password = $host->{Telnet_Password} || $conf->{general}->{password};
   
   $l->p("Connecting to $fox");    if ($host->{group} !~ /Client$/) {
   unless ( $t->open($fox) ) {      $l->p("Logging in");
     $l->sp("Error connecting: $!");      $t->login($password);
     next;      unless ($t->logged_in) {
   }        $l->p('Failed!');
         $t->close;
   if ($t->host_type ne $conf->{'type'}) {        next;
     $l->sp("Wrong type of unit ('$t->host_type' should be '$conf->{'type'}')");      }
     $t->close;  
     next;      $l->sp("Getting sudb");
   }      my $sudb = $t->sudb_view;
       if ($sudb) {
   if ($t->firmware_version eq $conf->{'ver'}) {        foreach my $su (@{ $sudb }) {
     $l->sp("Already up to date with firmware version '$t->firmware_version'");          $l->p("Getting su info $su->{suid}");
     $t->close;          my $su_info = $t->su_info( $su->{suid} );
     next;          if ($su_info->{ip}) {
   }            $l->sp("Adding host $su_info->{ip}");
             my $new_host = {
   $l->p("Logging in");              Telnet_Password => $host->{Telnet_Password},
   $t->login($conf->{'password'}) || die "Couldn't login: $!";              group   => $host->{group} . '-Client',
               name    => $su_info->{ip},
   $l->p("Sending commands");              remarks => $su_info->{remarks},
   ## Send commands            };
   #print Dump $t->login_banner;            push @{ $hosts }, $new_host;
   #print Dump $t->sysinfo;          } else {
   #print Dump $t->last_lines;            $l->sp("Couldn't get su info for $su->{suid}");
   #print Dump $t->ver();            if ($su_info->{ERR}) {
   print Dump $t->tftpd();              $l->sp("ERR: $su_info->{ERR}");
   print Dump $t->enable_tftpd();            }
   #print Dump [ "Exit", $t->exit ];            next;
   print Dump $t->tftpd();          }
   print Dump $t->disable_tftpd();        }
   print Dump $t->tftpd();      }
   #if ( upload($t, $conf->{'file_name'}) ) {    }
   #   $l->p("Rebooting");  
   #  $t->reboot;    foreach my $firmware_type ('Firmware', 'FPGA') {
   #} else {  
     $l->p("Exiting");      if (! exists $conf->{$firmware_type}) {
     $t->exit;            $l->s("No configs for '$firmware_type'");
   #}            $t->close;
               next;
   $l->sp("");          }
 }  
       my $host_type = $t->host_type;
 sub upload      if ($firmware_type eq 'FPGA') {
 {        $host_type =~ s/\s.*$//;
   my $t    = shift;      }
   my $file = shift;  
       if (! exists $conf->{$firmware_type}->{$host_type}) {
   my $ver = $t->ver;            $l->sp("No '$firmware_type' config for type $host_type");
   $l->p("Current version '$ver->{'Firmware Version'}'");            $t->close;
             next;
   if (          }
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&  
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}      if (! $t->logged_in) {
   ) {        $l->p("Logging in");
     $l->sp("Already updated!");        $t->login($password);
     return 1;        unless ($t->logged_in) {
   }          $l->p('Failed!');
           $t->close;
   my $try = 0;          next;
   while (1) {        }
     if ($try >= $max_tries) {      }
       $l->sp("Couldn't update in $max_tries tries!");  
       return undef;          foreach my $k (keys %{ $conf->{general} }) {
     }            $conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k};
     $try++;      }
           $conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type;
     #sysinfo($self->{'_host'});          $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
       
     $l->p("Enabling TFTPd");      $l->sp("$host_type $firmware_type");
     $t->enable_tftpd || die "Couldn't enable tftpd";      $l->p("Sending commands");
       ## Send commands
     $l->p("Uploading file ($file)");      my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
     # use tftp to push the file up          if ($rc) {
     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');        $l->sp("Successfull!");
             $needs_reboot = 1;
     $tftp->put($file, $file)      } elsif (defined $rc) {
       or die "Error uploading: " . $tftp->error;            $l->sp("Already up to date");
       } else {
     # waitfor some sort of output            $l->sp("Failed");
     # make sure it says 'Success.' otherwise error            $t->bye;
     #print "LAST: " . $self->lastline;            next;
     #my @lines = $self->getlines;      }
     #print Dump \@lines;  
         }
     $l->p("Checking upload ($conf->{'file_cksum'})");  
     my $results = $t->tftpd;    if ($needs_reboot) {
     # check the 'File Length' against ???      $l->sp("Rebooting $host->{name}");
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {      $t->reboot;
       $l->sp(    } else {
         "File checksum '" . $results->{'File Checksum'} .      $l->sp("Bye $host->{name}");
         "does not match config file '" . $conf->{'file_cksum'} . "'!"      $t->bye();
       );    }
       next;  }
     }  
     $l->p("File checksum matches . . . ");  sub upload
   {
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {    my $t    = shift;
       $l->sp(    my $conf = shift;
         "File length '" . $results->{'File Length'} .  
         "does not match config file '" . $conf->{'file_size'} . " bytes'!"    my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
       );  
       next;    my $fw_type = $conf->{firmware_type};
     }  
     $l->p("File length matches . . . ");    my $ver = $t->ver;
   
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {    if (! (
       $l->sp(      $ver->{$fw_type . ' Version'}  &&
         "File name '" . $results->{'File Name'} .      $ver->{$fw_type . ' Checksum'}
         "' does not match config file '" . $conf->{'file_name'} . "'!"    )) {
       );      $l->sp("Error getting current version numbers");
       next;          return;
     }    }
     $l->p("File name  matches . . . ");  
     if (
     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");      $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
     unless ($results = $t->updateflash(      $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
       args => 'mainimage ' . $ver->{'Firmware Checksum'} .    ) {
               ' '          . $conf->{'cksum'},      return 0;
       Timeout => 90,    }
     ) ) {  
       $l->sp("Couldn't update flash: $!");    $l->sp("Config information:");
       next;    $l->sp("  Hardware Type: $conf->{'type'}");
     }    $l->sp("  File Name:     $conf->{'file_name'}");
     $l->sp("  File Size:     $conf->{'file_size'}");
     unless (    $l->sp("  File Checksum: $conf->{'file_cksum'}");
       defined $results->{'Checksum'} &&    $l->sp("  Conf Version:  $conf->{'ver'}");
       $results->{'Checksum'} eq $conf->{'cksum'}    $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
     ) {    $l->sp("  Conf Checksum: $conf->{'cksum'}");
       $l->sp("Saved checksum does not match config file!");    $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
       next;  
     }    $l->sp("Updating");
     $l->p("Uploaded checksum ($results->{'Checksum'}) " .    my $try = 0;
           "matches ($conf->{'cksum'})");    while (1) {
           if ($try >= $max_tries) {
     $l->sp("Successfully updated!");        $l->sp("Couldn't update in $max_tries tries!");
     return 1;        return;
   }      }
 }      $try++;
   
 sub read_conf      #sysinfo($self->{'_host'});
 {  
   my $file = shift;      $l->p("Enabling TFTPd");
   my %conf;      unless ($t->enable_tftpd) {
   my $in_ip_list = 0;        $l->sp("Couldn't enable tftpd");
   open my $fh, $file or die "Couldn't open file $file: $!";        next;
   while (<$fh>) {      }
     chomp;  
     next if /^#/;      $l->p("Uploading file ($file)");
     next if /^$/;      # use tftp to push the file up
     if ($in_ip_list) {      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
       s/\s+//g; # Whitespace is a no no  
       unless ($tftp->put($file, $file)) {
       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {        $l->sp("Error uploading: " . $tftp->error);
         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);        next;
       } else {      }
         push @{ $conf{'ips'} }, $_;  
       }      $l->p("Checking upload ($conf->{'file_cksum'})");
     } else {      my $results = $t->tftpd;
       my ($key, $val) = split /\s+/, $_, 2;      # check the 'File Length' against ???
       if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
       if (lc($key) eq 'ips') {        $l->sp(
         $in_ip_list = 1;          "File checksum '" . $results->{'File Checksum'} .
         next;          " does not match config file '" . $conf->{'file_cksum'} . "'!"
       }        );
         next;
       $key =~ s/^\s+//;      }
       $key =~ s/\s+$//;      $l->p("File checksum matches . . . ");
       $val =~ s/^\s+//;  
       $val =~ s/\s+$//;      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
         $l->sp(
       $conf{ lc($key) } = $val;          "File length '" . $results->{'File Length'} .
     }          "does not match config file '" . $conf->{'file_size'} . " bytes'!"
   }        );
   close $fh;        next;
       }
   #print Dump \%conf;      $l->p("File length matches . . . ");
   foreach (  
     'password',      if ( uc($results->{'File Name'}) ne uc($file) ) {
     'file_name', 'file_size', 'file_cksum',        $l->sp(
     'ver', 'cksum', 'ips',          "File name '" . $results->{'File Name'} .
   ) {          "' does not match config file '" . $file . "'!"
     die "No $_ specified in config file!"        );
       if (not exists $conf{$_});        next;
   }      }
       $l->p("File name  matches . . . ");
   #print Dump \%conf;  
   #exit;      my $image_type = 'mainimage';
   return \%conf;      if ($fw_type eq 'FPGA') {
 }        $image_type = 'fpgaimage';
       }
 package Mylogger;      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
       unless ($results = $t->updateflash(
 use Fcntl ':flock'; # import LOCK_* constants        args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
 #use YAML;                ' '          . $conf->{'cksum'},
 use constant LOG_PRINT => 128;        Timeout => 90,
 use constant LOG_SAVE  =>  64;      ) ) {
         $l->sp("Couldn't update flash: $!");
 DESTROY {        next;
   my $self = shift;      }
   if ($self->{'MYLOG'}) {  
     $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");      unless (
     close $self->{'MYLOG'};        defined $results->{'Checksum'} &&
   }        $results->{'Checksum'} eq $conf->{'cksum'}
 }      ) {
         $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
 sub new {        next;
   my $package = shift;      }
   my $self = shift || {};      $l->p("Uploaded checksum ($results->{'Checksum'}) " .
             "matches ($conf->{'cksum'})");
   $self->{'base_path'}  ||= '.';  
   $self->{'log_path'}   ||= $self->{'base_path'};      $l->sp("Successfully updated!");
   $self->{'log_prefix'} ||= 'LOG';      return 1;
   $self->{'log_file'}   ||= GetLogName(    }
     $self->{'log_prefix'},  }
     $self->{'log_path'}  
   );  package Mylogger;
   bless $self, $package;  
 }  use Fcntl ':flock'; # import LOCK_* constants
   #use YAML;
 sub s  use constant LOG_PRINT => 128;
 {  use constant LOG_SAVE  =>  64;
   my $self = shift;  
   my $m = shift;  DESTROY {
   return $self->mylog($m, LOG_SAVE);    my $self = shift;
 }    if ($self->{'MYLOG'}) {
       $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
 sub p      close $self->{'MYLOG'};
 {    }
   my $self = shift;  }
   my $m = shift;  
   return $self->mylog($m, LOG_PRINT);  sub new {
 }    my $package = shift;
     my $self = shift || {};
 sub sp  
 {    $self->{'base_path'}  ||= '.';
   my $self = shift;    $self->{'log_path'}   ||= $self->{'base_path'};
   my $m = shift;    $self->{'log_prefix'} ||= 'LOG';
   return $self->mylog($m, LOG_SAVE | LOG_PRINT);    $self->{'log_file'}   ||= GetLogName(
 }      $self->{'log_prefix'},
       $self->{'log_path'}
 sub mylog    );
 {    bless $self, $package;
   my $self = shift;  }
   
   my $thing = shift;  sub s
   chomp $thing;  {
     my $self = shift;
   my $which = shift;    my $m = shift;
     return $self->mylog($m, LOG_SAVE);
   my $MYLOG;  }
   if ($which & LOG_PRINT) {  
     print $thing, "\n";  sub p
   }  {
     my $self = shift;
   if ($which & LOG_SAVE) {    my $m = shift;
     if ($self->{'MYLOG'}) {    return $self->mylog($m, LOG_PRINT);
       $MYLOG = $self->{'MYLOG'};  }
     } else {  
       unless ($MYLOG) {  sub sp
                 open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})  {
           or die "Couldn't open logfile!\n";    my $self = shift;
         my $ofh = select $MYLOG;    my $m = shift;
         $|=1;    return $self->mylog($m, LOG_SAVE | LOG_PRINT);
         select $ofh;  }
         $self->{'MYLOG'} = $MYLOG;  
   sub mylog
         $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");  {
       }    my $self = shift;
     }  
     flock($MYLOG, LOCK_EX);    my $thing = shift;
     print $MYLOG (scalar gmtime), "\t", $thing, "\n"    chomp $thing;
       or die "Couldn't print to MYLOG: $!";  
     flock($MYLOG, LOCK_UN);    my $which = shift;
   }  
 }    my $MYLOG;
     if ($which & LOG_PRINT) {
 sub GetLogName      print $thing, "\n";
 {    }
   my $prefix  = shift || die "Invalid prefix passed for log";  
     if ($which & LOG_SAVE) {
   my $logdate = GetLogDate();      if ($self->{'MYLOG'}) {
   my $logver  = 0;        $MYLOG = $self->{'MYLOG'};
   my $logname;      } else {
         unless ($MYLOG) {
   do {                  open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';            or die "Couldn't open logfile!\n";
     $logver++;          my $ofh = select $MYLOG;
   } until (not -e $logname);          $|=1;
           select $ofh;
   return $logname;          $self->{'MYLOG'} = $MYLOG;
 }  
           $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
 sub GetLogDate        }
 {      }
   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();      flock($MYLOG, LOCK_EX);
       print $MYLOG (scalar gmtime), "\t", $thing, "\n"
   $mon++;        or die "Couldn't print to MYLOG: $!";
   $year += 1900;      flock($MYLOG, LOCK_UN);
     }
   if ($min  < 10) { $min  = "0$min"  }  }
   if ($sec  < 10) { $sec  = "0$sec"  }  
   if ($hour < 10) { $hour = "0$hour" }  sub GetLogName
   if ($mday < 10) { $mday = "0$mday" }  {
   if ($mon  < 10) { $mon  = "0$mon"  }    my $prefix  = shift || die "Invalid prefix passed for log";
   
   my $time = $year . $mon . $mday;    my $logdate = GetLogDate();
     my $logver  = 0;
   return $time;    my $logname;
 }  
     do {
       $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
       $logver++;
     } until (not -e $logname);
   
     return $logname;
   }
   
   sub GetLogDate
   {
     my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
   
     $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.14  
changed lines
  Added in v.1.26

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