[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.4 and 1.21

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

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