[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.3 and 1.22

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

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.22

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