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

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

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