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

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

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