[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.6 and 1.25

version 1.6, 2005/11/17 00:40: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.5 2005/11/16 21:39:51 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::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'}  eq $conf->{'ver'} &&
       mylog("File length does not match config file!", LOG_SAVE);      $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
       next;    ) {
     }      return 0;
     mylog("File length ($results->{'File Length'}) " .    }
           "matches ($conf->{'file_size'})");  
     $l->sp("Config information:");
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {    $l->sp("  Hardware Type: $conf->{'type'}");
       mylog("File name does not match config file!", LOG_SAVE);    $l->sp("  File Name:     $conf->{'file_name'}");
       next;    $l->sp("  File Size:     $conf->{'file_size'}");
     }    $l->sp("  File Checksum: $conf->{'file_cksum'}");
     mylog("File name ($results->{'File Name'}) " .    $l->sp("  Conf Version:  $conf->{'ver'}");
           "matches ($conf->{'file_name'})");    $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
     $l->sp("  Conf Checksum: $conf->{'cksum'}");
     mylog("Updating flash (new checksum '$conf->{'cksum'}')");    $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
     $results = updateflash(  
       $host, $ver->{'Firmware Checksum'}, $conf->{'cksum'}    $l->sp("Updating");
     ) or die "Couldn't update flash: " . $host->lastline;    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;
       mylog("Saved checksum does not match config file!", LOG_SAVE);      }
       next;      $try++;
     }  
     mylog("Uploaded checksum ($results->{'Checksum'}) " .      #sysinfo($self->{'_host'});
           "matches ($conf->{'cksum'})");  
           $l->p("Enabling TFTPd");
     mylog("Successfully updated!", LOG_SAVE);      unless ($t->enable_tftpd) {
     return 1;        $l->sp("Couldn't enable tftpd");
   }        next;
 }      }
   
 sub get_ver      $l->p("Uploading file ($file)");
 {      # use tftp to push the file up
   my $host = shift;      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
   return cmd($host, 'ver');  
 }      unless ($tftp->put($file, $file)) {
         $l->sp("Error uploading: " . $tftp->error);
 sub enable_tftpd        next;
 {      }
   my $host = shift;  
       $l->p("Checking upload ($conf->{'file_cksum'})");
   my $vals = cmd($host, 'tftpd on', 'Success.');      my $results = $t->tftpd;
       # check the 'File Length' against ???
   if ($vals->{'Tftpd'} eq 'listen') {      if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
     return $vals;        $l->sp(
   } else {          "File checksum '" . $results->{'File Checksum'} .
     return undef;          " does not match config file '" . $conf->{'file_cksum'} . "'!"
   }        );
 }        next;
       }
 sub check_tftpd      $l->p("File checksum matches . . . ");
 {  
   my $host = shift;      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
   return cmd($host, 'tftpd', 'Success.');        $l->sp(
 }          "File length '" . $results->{'File Length'} .
           "does not match config file '" . $conf->{'file_size'} . " bytes'!"
 sub sysinfo        );
 {        next;
   my $host = shift;      }
   return cmd($host, 'sysinfo', 'Success.');      $l->p("File length matches . . . ");
 }  
       if ( uc($results->{'File Name'}) ne uc($file) ) {
 sub updateflash        $l->sp(
 {          "File name '" . $results->{'File Name'} .
   my $host = shift;          "' does not match config file '" . $file . "'!"
   my $old = shift;        );
   my $new = shift;        next;
       }
   return undef unless $new;      $l->p("File name  matches . . . ");
   
   return cmd($host, "updateflash mainimage $old $new", 'Success.', 90);      my $image_type = 'mainimage';
 }      if ($fw_type eq 'FPGA') {
         $image_type = 'fpgaimage';
 sub cmd      }
 {      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
   my $host   = shift;      unless ($results = $t->updateflash(
   my $string = shift;        args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
   my $expect_last = shift;                ' '          . $conf->{'cksum'},
   my $timeout = shift || 5;        Timeout => 90,
       ) ) {
   my @lines = $host->cmd(String => $string, Timeout => $timeout);        $l->sp("Couldn't update flash: $!");
         next;
   my $vals = decode_lines(@lines);      }
   
   my $last = $host->lastline;      unless (
         defined $results->{'Checksum'} &&
   unless ($expect_last) {        $results->{'Checksum'} eq $conf->{'cksum'}
     return $vals;      ) {
   }        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
         next;
   if ($last =~ /$expect_last$/) {      }
     return $vals;      $l->p("Uploaded checksum ($results->{'Checksum'}) " .
   } else {            "matches ($conf->{'cksum'})");
     warn "Error with command ($string): $last";  
     return undef;      $l->sp("Successfully updated!");
   }      return 1;
 }    }
   }
 sub decode_lines  
 {  package Mylogger;
   my @lines = @_;  
   use Fcntl ':flock'; # import LOCK_* constants
   my %conf;  #use YAML;
   use constant LOG_PRINT => 128;
   my $key = '';  use constant LOG_SAVE  =>  64;
   my $val = '';  
   my $in_key = 0;  DESTROY {
   my $in_val = 0;    my $self = shift;
     if ($self->{'MYLOG'}) {
   foreach my $line (@lines) {      $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
     my @chars = split //, $line;      close $self->{'MYLOG'};
     }
     my $last_key = '';  }
     foreach my $c (@chars) {  
   sub new {
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {    my $package = shift;
         if ($c eq '[') {    my $self = shift || {};
           $in_key = 1;  
           $in_val = 0;    $self->{'base_path'}  ||= '.';
         } else {    $self->{'log_path'}   ||= $self->{'base_path'};
           $in_key = 0;    $self->{'log_prefix'} ||= 'LOG';
           $in_val = 0;    $self->{'log_file'}   ||= GetLogName(
         }      $self->{'log_prefix'},
       $self->{'log_path'}
         if ($key) {    );
           $key =~ s/^\s+//;    bless $self, $package;
           $key =~ s/\s+$//;  }
   
           $val =~ s/^\s+//;  sub s
           $val =~ s/\s+$//;  {
     my $self = shift;
           if ($key eq 'Checksum' && $last_key) {    my $m = shift;
             # Special case for these bastids.    return $self->mylog($m, LOG_SAVE);
             my $new = $last_key;  }
             $new =~ s/\s+\S+$//;  
             $key = $new . " " . $key;  sub p
           }  {
     my $self = shift;
           $last_key = $key;    my $m = shift;
           $conf{$key} = $val;    return $self->mylog($m, LOG_PRINT);
           $key = '';  }
           $val = '';  
         }  sub sp
   {
       } elsif ($c eq ']') {    my $self = shift;
         $in_val = 1;    my $m = shift;
         $in_key = 0;    return $self->mylog($m, LOG_SAVE | LOG_PRINT);
         $c = shift @chars;  }
   
       } elsif ($in_key) {  sub mylog
         $key .= $c;  {
     my $self = shift;
       } elsif ($in_val) {  
         $val .= $c;    my $thing = shift;
       }    chomp $thing;
     }  
   }    my $which = shift;
   #print Dump \%conf;  
     my $MYLOG;
   if (%conf) {    if ($which & LOG_PRINT) {
     return \%conf;      print $thing, "\n";
   } else {    }
     return \@lines;  
   }    if ($which & LOG_SAVE) {
 }      if ($self->{'MYLOG'}) {
         $MYLOG = $self->{'MYLOG'};
 sub read_conf      } else {
 {        unless ($MYLOG) {
   my $file = shift;                  open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
   my %conf;            or die "Couldn't open logfile!\n";
   my $in_ip_list = 0;          my $ofh = select $MYLOG;
   open my $fh, $file or die "Couldn't open file $file: $!";          $|=1;
   while (<$fh>) {          select $ofh;
     chomp;          $self->{'MYLOG'} = $MYLOG;
     next if /^#/;  
     next if /^$/;          $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
     if ($in_ip_list) {        }
       push @{ $conf{'ips'} }, $_;      }
     } else {      flock($MYLOG, LOCK_EX);
       my ($key, $val) = split /\s+/, $_, 2;      print $MYLOG (scalar gmtime), "\t", $thing, "\n"
         or die "Couldn't print to MYLOG: $!";
       if (lc($key) eq 'ips') {      flock($MYLOG, LOCK_UN);
         $in_ip_list = 1;    }
         next;  }
       }  
   sub GetLogName
       $conf{ lc($key) } = $val;  {
     }    my $prefix  = shift || die "Invalid prefix passed for log";
   }  
   close $fh;    my $logdate = GetLogDate();
     my $logver  = 0;
   #print Dump \%conf;    my $logname;
   foreach (  
     'password',    do {
     'file_name', 'file_size', 'file_cksum',      $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
     'ver', 'cksum', 'ips',      $logver++;
   ) {    } until (not -e $logname);
     die "No $_ specified in config file!"  
       if (not exists $conf{$_});    return $logname;
   }  }
   
   return \%conf;  sub GetLogDate
 }  {
     my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
 sub mylog  
 {    $mon++;
   my $thing = shift;    $year += 1900;
   chomp $thing;  
   my $save = shift;    if ($min  < 10) { $min  = "0$min"  }
     if ($sec  < 10) { $sec  = "0$sec"  }
   print $thing, "\n";    if ($hour < 10) { $hour = "0$hour" }
     if ($mday < 10) { $mday = "0$mday" }
   if (defined $save && $save == LOG_SAVE) {    if ($mon  < 10) { $mon  = "0$mon"  }
     unless ($MYLOG) {  
       open ($MYLOG, '>>', $log_file)    my $time = $year . $mon . $mday;
         or die "Couldn't open logfile!\n";  
       my $ofh = select $MYLOG;    return $time;
       $|=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.6  
changed lines
  Added in v.1.25

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