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

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

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