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

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

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