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

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

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