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

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

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