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

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

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