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

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

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