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

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

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