[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.7 and 1.26

version 1.7, 2005/11/17 20:09:45 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.6 2005/11/17 00:40:06 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::Telnet;  use YAML qw/ LoadFile /;
 use Net::TFTP;  use Net::TFTP;
 use Fcntl ':flock'; # import LOCK_* constants  use lib '.';
 #use YAML;  use Net::Telnet::Trango;
   use RedRiver::Wireless;
   
 my $config_file = shift || 'update_trango.conf';  my $config_file = shift || 'update_trango.yaml';
 my $conf = read_conf($config_file);  my $max_tries = 3;
   
 my $max_tries = 1;  my $l = Mylogger->new( { log_prefix => 'UT' } );
   
 my $log_file =  GetLogName('UT');  $l->sp("Reading config file '$config_file'");
 use constant LOG_SAVE => 1;  my $conf = LoadFile($config_file);
   
 my $MYLOG;  # file handle for logging so we don't have to close and open it all the time  $l->sp("Reading hosts");
 END {  my $hosts = RedRiver::Wireless::Read_Hosts();
   if ($MYLOG) {  
     mylog("Closing log ($log_file)");  
     close $MYLOG;  
   }  foreach my $host (@{ $hosts }) {
 }    next if $host->{group} !~ /^Trango/;
     $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: $!";
   mylog("Updating: $fox", LOG_SAVE);    $l->p("Connecting to $host->{name}");
     unless ( $t->open($host->{name}) ) {
   ## Connect and login.      $l->sp("Error connecting: $!");
   my $host = new Net::Telnet (Timeout => 5,      next;
                               Prompt => '/#> *$/');    }
   mylog("Connecting to $fox");  
   unless ( $host->open( Host => $fox, Errmode => 'return') ) {    my $password = $host->{Telnet_Password} || $conf->{general}->{password};
     mylog("Couldn't connect to $fox.  Connection timed out.", LOG_SAVE);  
     next;    if ($host->{group} !~ /Client$/) {
   }      $l->p("Logging in");
   $host->dump_log('dump.log');      $t->login($password);
       unless ($t->logged_in) {
   ## Login to remote host.        $l->p('Failed!');
   unless ($host->waitfor(        $t->close;
     -match => '/password: ?$/i',        next;
     -errmode => "return",      }
   ) ) {  
     mylog("problem connecting to host ($fox): " . $host->lastline, LOG_SAVE);      $l->sp("Getting sudb");
     next;      my $sudb = $t->sudb_view;
   }      if ($sudb) {
         foreach my $su (@{ $sudb }) {
   my $login_banner = $host->lastline;          $l->p("Getting su info $su->{suid}");
   my ($type, $version) = $login_banner =~          my $su_info = $t->su_info( $su->{suid} );
     /Welcome to Trango Broadband Wireless (\w+)-(.+)$/i;          if ($su_info->{ip}) {
             $l->sp("Adding host $su_info->{ip}");
   if ($type ne $conf->{'type'}) {            my $new_host = {
     mylog("Wrong type of unit ('$type' should be '$conf->{'type'}')", LOG_SAVE);              Telnet_Password => $host->{Telnet_Password},
     $host->close;              group   => $host->{group} . '-Client',
     next;              name    => $su_info->{ip},
   }              remarks => $su_info->{remarks},
             };
   if ($version eq $conf->{'ver'}) {            push @{ $hosts }, $new_host;
     mylog("Already up to date with firmware version '$version'", LOG_SAVE);          } else {
     $host->close;            $l->sp("Couldn't get su info for $su->{suid}");
     next;            if ($su_info->{ERR}) {
   }              $l->sp("ERR: $su_info->{ERR}");
             }
   mylog("Logging in");            next;
   $host->print($conf->{'password'});          }
   unless ($host->waitfor(        }
     -match => $host->prompt,      }
     -errmode => "return",    }
   ) ) {  
     mylog("login ($fox) failed: " . $host->lastline);    foreach my $firmware_type ('Firmware', 'FPGA') {
     next;  
   }      if (! exists $conf->{$firmware_type}) {
             $l->s("No configs for '$firmware_type'");
             $t->close;
   mylog("Sending commands");            next;
   ## Send commands          }
   if ( upload($host, $fox, $conf->{'file_name'}) ) {  
     mylog("Rebooting");      my $host_type = $t->host_type;
     $host->print("reboot\n");      if ($firmware_type eq 'FPGA') {
     $host->getline;        $host_type =~ s/\s.*$//;
   } else {      }
     mylog("Exiting");  
     $host->print("exit\n");      if (! exists $conf->{$firmware_type}->{$host_type}) {
     $host->getline;            $l->sp("No '$firmware_type' config for type $host_type");
   }            $t->close;
   $host->close;            next;
   mylog("", LOG_SAVE);          }
 }  
       if (! $t->logged_in) {
 sub upload        $l->p("Logging in");
 {        $t->login($password);
   my $host = shift;        unless ($t->logged_in) {
   my $fox  = shift;          $l->p('Failed!');
   my $file = shift;          $t->close;
           next;
   mylog("Getting current version");        }
   my $ver = get_ver($host);      }
   
   if (          foreach my $k (keys %{ $conf->{general} }) {
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&            $conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k};
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}      }
   ) {          $conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type;
     mylog("Already updated!", LOG_SAVE);          $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
     return 1;  
   }      $l->sp("$host_type $firmware_type");
       $l->p("Sending commands");
   my $try = 0;      ## Send commands
   while (1) {      my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
     if ($try >= $max_tries) {          if ($rc) {
       mylog("Couldn't update in $max_tries tries!");        $l->sp("Successfull!");
       return undef;            $needs_reboot = 1;
     }      } elsif (defined $rc) {
     $try++;            $l->sp("Already up to date");
       } else {
     #sysinfo($host);            $l->sp("Failed");
                 $t->bye;
     mylog("Enabling TFTPd");            next;
     enable_tftpd($host) || die "Couldn't enable tftpd";      }
   
     mylog("Uploading file ($file)");    }
     # use tftp to push the file up  
     my $tftp = Net::TFTP->new($fox, Mode => 'octet');    if ($needs_reboot) {
       $l->sp("Rebooting $host->{name}");
     $tftp->put($file, $file)      $t->reboot;
       or die "Error uploading: " . $tftp->error;    } else {
       $l->sp("Bye $host->{name}");
     # waitfor some sort of output      $t->bye();
     # make sure it says 'Success.' otherwise error    }
     #print "LAST: " . $host->lastline;  }
     #my @lines = $host->getlines;  
     #print Dump \@lines;  sub upload
       {
     mylog("Checking upload ($conf->{'file_cksum'})");    my $t    = shift;
     my $results = check_tftpd($host);    my $conf = shift;
     # check the 'File Length' against ???  
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {    my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
       mylog("File checksum does not match config file!", LOG_SAVE);  
       next;    my $fw_type = $conf->{firmware_type};
     }  
     mylog("File checksum ($results->{'File Checksum'}) " .    my $ver = $t->ver;
           "matches ($conf->{'file_cksum'})");  
     if (! (
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {      $ver->{$fw_type . ' Version'}  &&
       mylog("File length does not match config file!", LOG_SAVE);      $ver->{$fw_type . ' Checksum'}
       next;    )) {
     }      $l->sp("Error getting current version numbers");
     mylog("File length ($results->{'File Length'}) " .          return;
           "matches ($conf->{'file_size'})");    }
   
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {    if (
       mylog("File name does not match config file!", LOG_SAVE);      $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
       next;      $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
     }    ) {
     mylog("File name ($results->{'File Name'}) " .      return 0;
           "matches ($conf->{'file_name'})");    }
   
     mylog("Updating flash (new checksum '$conf->{'cksum'}')");    $l->sp("Config information:");
     $results = updateflash(    $l->sp("  Hardware Type: $conf->{'type'}");
       $host, $ver->{'Firmware Checksum'}, $conf->{'cksum'}    $l->sp("  File Name:     $conf->{'file_name'}");
     ) or die "Couldn't update flash: " . $host->lastline;    $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'}");
       mylog("Saved checksum does not match config file!", LOG_SAVE);    $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
       next;  
     }    $l->sp("Updating");
     mylog("Uploaded checksum ($results->{'Checksum'}) " .    my $try = 0;
           "matches ($conf->{'cksum'})");    while (1) {
           if ($try >= $max_tries) {
     mylog("Successfully updated!", LOG_SAVE);        $l->sp("Couldn't update in $max_tries tries!");
     return 1;        return;
   }      }
 }      $try++;
   
 sub get_ver      #sysinfo($self->{'_host'});
 {  
   my $host = shift;      $l->p("Enabling TFTPd");
   return cmd($host, 'ver');      unless ($t->enable_tftpd) {
 }        $l->sp("Couldn't enable tftpd");
         next;
 sub enable_tftpd      }
 {  
   my $host = shift;      $l->p("Uploading file ($file)");
       # use tftp to push the file up
   my $vals = cmd($host, 'tftpd on', 'Success.');      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
   
   if ($vals->{'Tftpd'} eq 'listen') {      unless ($tftp->put($file, $file)) {
     return $vals;        $l->sp("Error uploading: " . $tftp->error);
   } else {        next;
     return undef;      }
   }  
 }      $l->p("Checking upload ($conf->{'file_cksum'})");
       my $results = $t->tftpd;
 sub check_tftpd      # check the 'File Length' against ???
 {      if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
   my $host = shift;        $l->sp(
   return cmd($host, 'tftpd', 'Success.');          "File checksum '" . $results->{'File Checksum'} .
 }          " does not match config file '" . $conf->{'file_cksum'} . "'!"
         );
 sub sysinfo        next;
 {      }
   my $host = shift;      $l->p("File checksum matches . . . ");
   return cmd($host, 'sysinfo', 'Success.');  
 }      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
         $l->sp(
 sub updateflash          "File length '" . $results->{'File Length'} .
 {          "does not match config file '" . $conf->{'file_size'} . " bytes'!"
   my $host = shift;        );
   my $old = shift;        next;
   my $new = shift;      }
       $l->p("File length matches . . . ");
   return undef unless $new;  
       if ( uc($results->{'File Name'}) ne uc($file) ) {
   return cmd($host, "updateflash mainimage $old $new", 'Success.', 90);        $l->sp(
 }          "File name '" . $results->{'File Name'} .
           "' does not match config file '" . $file . "'!"
 sub cmd        );
 {        next;
   my $host   = shift;      }
   my $string = shift;      $l->p("File name  matches . . . ");
   my $expect_last = shift;  
   my $timeout = shift || 5;      my $image_type = 'mainimage';
       if ($fw_type eq 'FPGA') {
   my @lines = $host->cmd(String => $string, Timeout => $timeout);        $image_type = 'fpgaimage';
       }
   my $vals = decode_lines(@lines);      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
       unless ($results = $t->updateflash(
   my $last = $host->lastline;        args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
                 ' '          . $conf->{'cksum'},
   unless ($expect_last) {        Timeout => 90,
     return $vals;      ) ) {
   }        $l->sp("Couldn't update flash: $!");
         next;
   if ($last =~ /$expect_last$/) {      }
     return $vals;  
   } else {      unless (
     warn "Error with command ($string): $last";        defined $results->{'Checksum'} &&
     return undef;        $results->{'Checksum'} eq $conf->{'cksum'}
   }      ) {
 }        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
         next;
 sub decode_lines      }
 {      $l->p("Uploaded checksum ($results->{'Checksum'}) " .
   my @lines = @_;            "matches ($conf->{'cksum'})");
   
   my %conf;      $l->sp("Successfully updated!");
       return 1;
   my $key = '';    }
   my $val = '';  }
   my $in_key = 0;  
   my $in_val = 0;  package Mylogger;
   
   foreach my $line (@lines) {  use Fcntl ':flock'; # import LOCK_* constants
     my @chars = split //, $line;  #use YAML;
   use constant LOG_PRINT => 128;
     my $last_key = '';  use constant LOG_SAVE  =>  64;
     foreach my $c (@chars) {  
   DESTROY {
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {    my $self = shift;
         if ($c eq '[') {    if ($self->{'MYLOG'}) {
           $in_key = 1;      $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
           $in_val = 0;      close $self->{'MYLOG'};
         } else {    }
           $in_key = 0;  }
           $in_val = 0;  
         }  sub new {
     my $package = shift;
         if ($key) {    my $self = shift || {};
           $key =~ s/^\s+//;  
           $key =~ s/\s+$//;    $self->{'base_path'}  ||= '.';
     $self->{'log_path'}   ||= $self->{'base_path'};
           $val =~ s/^\s+//;    $self->{'log_prefix'} ||= 'LOG';
           $val =~ s/\s+$//;    $self->{'log_file'}   ||= GetLogName(
       $self->{'log_prefix'},
           if ($key eq 'Checksum' && $last_key) {      $self->{'log_path'}
             # Special case for these bastids.    );
             my $new = $last_key;    bless $self, $package;
             $new =~ s/\s+\S+$//;  }
             $key = $new . " " . $key;  
           }  sub s
   {
           $last_key = $key;    my $self = shift;
           $conf{$key} = $val;    my $m = shift;
           $key = '';    return $self->mylog($m, LOG_SAVE);
           $val = '';  }
         }  
   sub p
       } elsif ($c eq ']') {  {
         $in_val = 1;    my $self = shift;
         $in_key = 0;    my $m = shift;
         $c = shift @chars;    return $self->mylog($m, LOG_PRINT);
   }
       } elsif ($in_key) {  
         $key .= $c;  sub sp
   {
       } elsif ($in_val) {    my $self = shift;
         $val .= $c;    my $m = shift;
       }    return $self->mylog($m, LOG_SAVE | LOG_PRINT);
     }  }
   }  
   #print Dump \%conf;  sub mylog
   {
   if (%conf) {    my $self = shift;
     return \%conf;  
   } else {    my $thing = shift;
     return \@lines;    chomp $thing;
   }  
 }    my $which = shift;
   
 sub read_conf    my $MYLOG;
 {    if ($which & LOG_PRINT) {
   my $file = shift;      print $thing, "\n";
   my %conf;    }
   my $in_ip_list = 0;  
   open my $fh, $file or die "Couldn't open file $file: $!";    if ($which & LOG_SAVE) {
   while (<$fh>) {      if ($self->{'MYLOG'}) {
     chomp;        $MYLOG = $self->{'MYLOG'};
     next if /^#/;      } else {
     next if /^$/;        unless ($MYLOG) {
     if ($in_ip_list) {                  open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {            or die "Couldn't open logfile!\n";
         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);          my $ofh = select $MYLOG;
       } else {          $|=1;
         push @{ $conf{'ips'} }, $_;          select $ofh;
       }          $self->{'MYLOG'} = $MYLOG;
     } else {  
       my ($key, $val) = split /\s+/, $_, 2;          $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
         }
       if (lc($key) eq 'ips') {      }
         $in_ip_list = 1;      flock($MYLOG, LOCK_EX);
         next;      print $MYLOG (scalar gmtime), "\t", $thing, "\n"
       }        or die "Couldn't print to MYLOG: $!";
       flock($MYLOG, LOCK_UN);
       $conf{ lc($key) } = $val;    }
     }  }
   }  
   close $fh;  sub GetLogName
   {
   #print Dump \%conf;    my $prefix  = shift || die "Invalid prefix passed for log";
   foreach (  
     'password',    my $logdate = GetLogDate();
     'file_name', 'file_size', 'file_cksum',    my $logver  = 0;
     'ver', 'cksum', 'ips',    my $logname;
   ) {  
     die "No $_ specified in config file!"    do {
       if (not exists $conf{$_});      $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
   }      $logver++;
     } until (not -e $logname);
   #print Dump \%conf;  
   #exit;    return $logname;
   return \%conf;  }
 }  
   sub GetLogDate
 sub mylog  {
 {    my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
   my $thing = shift;  
   chomp $thing;    $mon++;
   my $save = shift;    $year += 1900;
   
   print $thing, "\n";    if ($min  < 10) { $min  = "0$min"  }
     if ($sec  < 10) { $sec  = "0$sec"  }
   if (defined $save && $save == LOG_SAVE) {    if ($hour < 10) { $hour = "0$hour" }
     unless ($MYLOG) {    if ($mday < 10) { $mday = "0$mday" }
       open ($MYLOG, '>>', $log_file)    if ($mon  < 10) { $mon  = "0$mon"  }
         or die "Couldn't open logfile!\n";  
       my $ofh = select $MYLOG;    my $time = $year . $mon . $mday;
       $|=1;  
       select $ofh;    return $time;
       mylog("Opened log ($log_file)");  }
     }  
   
     flock($MYLOG, LOCK_EX);  
     print $MYLOG (scalar gmtime), "\t", $thing, "\n"  
       or die "Couldn't print to MYLOG: $!";  
     flock($MYLOG, LOCK_UN);  
   }  
 }  
   
 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.7  
changed lines
  Added in v.1.26

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