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

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

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