[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.13 and 1.18

version 1.13, 2005/12/30 01:02:41 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.12 2005/12/29 18:41:17 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::TFTP;  use Net::TFTP;
 use YAML;  use lib '.';
 use lib '.';  use Net::Telnet::Trango;
 use Net::Telnet::Trango;  
   my $config_file = shift || 'update_trango.conf';
 my $config_file = shift || 'update_trango.conf';  my $max_tries = 3;
 my $max_tries = 3;  
   
   
   my $l = Mylogger->new( { log_prefix => 'UT' } );
 my $l = Mylogger->new( { log_prefix => 'UT' } );  
   
   $l->sp("Reading config file '$config_file'");
 $l->sp("Reading config file '$config_file'");  my $conf = read_conf($config_file);
 my $conf = read_conf($config_file);  
   $l->sp("  Hardware Type: $conf->{'type'}");
 $l->sp("  Hardware Type: $conf->{'type'}");  $l->sp("  File Name:     $conf->{'file_name'}");
 $l->sp("  File Name:     $conf->{'file_name'}");  $l->sp("  File Size:     $conf->{'file_size'}");
 $l->sp("  File Size:     $conf->{'file_size'}");  $l->sp("  File Checksum: $conf->{'file_cksum'}");
 $l->sp("  File Checksum: $conf->{'file_cksum'}");  $l->sp("  FW Version:    $conf->{'ver'}");
 $l->sp("  FW Version:    $conf->{'ver'}");  $l->sp("  FW Checksum:   $conf->{'cksum'}");
 $l->sp("  FW Checksum:   $conf->{'cksum'}");  $l->sp("");
 $l->sp("");  
   
   
   
   
   
   foreach my $fox (@{ $conf->{'ips'} }) {
 foreach my $fox (@{ $conf->{'ips'} }) {    $l->sp("Updating: $fox");
   $l->sp("Updating: $fox");  
     ## Connect and login.
   ## Connect and login.    my $t = new Net::Telnet::Trango (
   my $t = new Net::Telnet::Trango ({      Timeout => 5,
     Host    => $fox,      Errmode => 'return',
     Timeout => 5,    ) or die "Couldn't make new connection: $!";
     Errmode => 'return',    $l->p("Connecting to $fox");
   }) or die "Couldn't make new connection: $!";    unless ( $t->open($fox) ) {
       $l->sp("Error connecting: $!");
   $l->p("Connecting to $fox");      next;
   unless ($t->open) {    }
     $l->sp("Error connecting: $!");  
     next;    if ($t->host_type ne $conf->{'type'}) {
   }      $l->sp("Wrong type of unit ('" . $t->host_type . "' should be '$conf->{'type'}')");
       $t->close;
   if ($t->host_type ne $conf->{'type'}) {      next;
     $l->sp("Wrong type of unit ('$t->host_type' should be '$conf->{'type'}')");    }
     $t->close;  
     next;    if ($t->firmware_version eq $conf->{'ver'}) {
   }      $l->sp("Already up to date with firmware version '" . $t->firmware_version . "'");
       $t->close;
   if ($t->firmware_version eq $conf->{'ver'}) {      next;
     $l->sp("Already up to date with firmware version '$t->firmware_version'");    }
     $t->close;  
     next;    $l->p("Logging in");
   }    $t->login($conf->{'password'}) || die "Couldn't login: $!";
   
   $l->p("Logging in");    $l->p("Sending commands");
   $t->login($conf->{'password'}) || die "Couldn't login: $!";    ## Send commands
     if ( upload($t, $conf->{'file_name'}) ) {
   $l->p("Sending commands");       $l->p("Rebooting");
   ## Send commands      $t->reboot;
   print Dump $t->login_banner;    } else {
   print Dump $t->sysinfo;      $l->p("Exiting");
   print Dump $t->last_lines;      $t->exit;
   print Dump $t->ver();    }
   print Dump $t->tftpd();  
   print Dump $t->enable_tftpd();    $l->sp("");
   print Dump [ "Exit", $t->exit ];  }
   print Dump $t->tftpd();  
   print Dump $t->disable_tftpd();  sub upload
   print Dump $t->tftpd();  {
   #if ( upload($t, $conf->{'file_name'}) ) {    my $t    = shift;
   #   $l->p("Rebooting");    my $file = shift;
   #  $t->reboot;  
   #} else {    my $ver = $t->ver;
     $l->p("Exiting");    $l->p("Current version '$ver->{'Firmware Version'}'");
     $t->exit;  
   #}    if (
         $ver->{'Firmware Version'}  eq $conf->{'ver'} &&
   $l->sp("");      $ver->{'Firmware Checksum'} eq $conf->{'cksum'}
 }    ) {
       $l->sp("Already updated!");
 sub upload      return 1;
 {    }
   my $t    = shift;  
   my $file = shift;    my $try = 0;
     while (1) {
   my $ver = $t->ver;      if ($try >= $max_tries) {
   $l->p("Current version '$ver->{'Firmware Version'}'");        $l->sp("Couldn't update in $max_tries tries!");
         return undef;
   if (      }
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&      $try++;
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}  
   ) {      #sysinfo($self->{'_host'});
     $l->sp("Already updated!");  
     return 1;      $l->p("Enabling TFTPd");
   }      $t->enable_tftpd || die "Couldn't enable tftpd";
   
   my $try = 0;      $l->p("Uploading file ($file)");
   while (1) {      # use tftp to push the file up
     if ($try >= $max_tries) {      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
       $l->sp("Couldn't update in $max_tries tries!");  
       return undef;      $tftp->put($file, $file)
     }        or die "Error uploading: " . $tftp->error;
     $try++;  
       # waitfor some sort of output
     #sysinfo($self->{'_host'});      # make sure it says 'Success.' otherwise error
           #print "LAST: " . $self->lastline;
     $l->p("Enabling TFTPd");      #my @lines = $self->getlines;
     $t->enable_tftpd || die "Couldn't enable tftpd";      #print Dump \@lines;
   
     $l->p("Uploading file ($file)");      $l->p("Checking upload ($conf->{'file_cksum'})");
     # use tftp to push the file up      my $results = $t->tftpd;
     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');      # check the 'File Length' against ???
       if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
     $tftp->put($file, $file)        $l->sp(
       or die "Error uploading: " . $tftp->error;          "File checksum '" . $results->{'File Checksum'} .
           "does not match config file '" . $conf->{'file_cksum'} . "'!"
     # waitfor some sort of output        );
     # make sure it says 'Success.' otherwise error        next;
     #print "LAST: " . $self->lastline;      }
     #my @lines = $self->getlines;      $l->p("File checksum matches . . . ");
     #print Dump \@lines;  
           if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
     $l->p("Checking upload ($conf->{'file_cksum'})");        $l->sp(
     my $results = $t->tftpd;          "File length '" . $results->{'File Length'} .
     # check the 'File Length' against ???          "does not match config file '" . $conf->{'file_size'} . " bytes'!"
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {        );
       $l->sp(        next;
         "File checksum '" . $results->{'File Checksum'} .      }
         "does not match config file '" . $conf->{'file_cksum'} . "'!"      $l->p("File length matches . . . ");
       );  
       next;      if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
     }        $l->sp(
     $l->p("File checksum matches . . . ");          "File name '" . $results->{'File Name'} .
           "' does not match config file '" . $conf->{'file_name'} . "'!"
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {        );
       $l->sp(        next;
         "File length '" . $results->{'File Length'} .      }
         "does not match config file '" . $conf->{'file_size'} . " bytes'!"      $l->p("File name  matches . . . ");
       );  
       next;      $l->p("Updating flash (new checksum '$conf->{'cksum'}')");
     }      unless ($results = $t->updateflash(
     $l->p("File length matches . . . ");        args => 'mainimage ' . $ver->{'Firmware Checksum'} .
                 ' '          . $conf->{'cksum'},
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {        Timeout => 90,
       $l->sp(      ) ) {
         "File name '" . $results->{'File Name'} .        $l->sp("Couldn't update flash: $!");
         "' does not match config file '" . $conf->{'file_name'} . "'!"        next;
       );      }
       next;  
     }      unless (
     $l->p("File name  matches . . . ");        defined $results->{'Checksum'} &&
         $results->{'Checksum'} eq $conf->{'cksum'}
     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");      ) {
     unless ($results = $t->updateflash(        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
       args => 'mainimage ' . $ver->{'Firmware Checksum'} .        next;
               ' '          . $conf->{'cksum'},      }
       Timeout => 90,      $l->p("Uploaded checksum ($results->{'Checksum'}) " .
     ) ) {            "matches ($conf->{'cksum'})");
       $l->sp("Couldn't update flash: $!");  
       next;      $l->sp("Successfully updated!");
     }      return 1;
     }
     unless (  }
       defined $results->{'Checksum'} &&  
       $results->{'Checksum'} eq $conf->{'cksum'}  sub read_conf
     ) {  {
       $l->sp("Saved checksum does not match config file!");    my $file = shift;
       next;    my %conf;
     }    my $in_ip_list = 0;
     $l->p("Uploaded checksum ($results->{'Checksum'}) " .    open my $fh, $file or die "Couldn't open file $file: $!";
           "matches ($conf->{'cksum'})");    while (<$fh>) {
           chomp;
     $l->sp("Successfully updated!");      next if /^#/;
     return 1;      next if /^$/;
   }      if ($in_ip_list) {
 }        s/\s+//g; # Whitespace is a no no
   
 sub read_conf        if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {
 {          push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);
   my $file = shift;        } else {
   my %conf;          push @{ $conf{'ips'} }, $_;
   my $in_ip_list = 0;        }
   open my $fh, $file or die "Couldn't open file $file: $!";      } else {
   while (<$fh>) {        my ($key, $val) = split /\s+/, $_, 2;
     chomp;  
     next if /^#/;        if (lc($key) eq 'ips') {
     next if /^$/;          $in_ip_list = 1;
     if ($in_ip_list) {          next;
       s/\s+//g; # Whitespace is a no no        }
   
       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {        $key =~ s/^\s+//;
         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);        $key =~ s/\s+$//;
       } else {        $val =~ s/^\s+//;
         push @{ $conf{'ips'} }, $_;        $val =~ s/\s+$//;
       }  
     } else {        $conf{ lc($key) } = $val;
       my ($key, $val) = split /\s+/, $_, 2;      }
     }
       if (lc($key) eq 'ips') {    close $fh;
         $in_ip_list = 1;  
         next;    #print Dump \%conf;
       }    foreach (
       'password',
       $key =~ s/^\s+//;      'file_name', 'file_size', 'file_cksum',
       $key =~ s/\s+$//;      'ver', 'cksum', 'ips',
       $val =~ s/^\s+//;    ) {
       $val =~ s/\s+$//;      die "No $_ specified in config file!"
         if (not exists $conf{$_});
       $conf{ lc($key) } = $val;    }
     }  
   }    #print Dump \%conf;
   close $fh;    #exit;
     return \%conf;
   #print Dump \%conf;  }
   foreach (  
     'password',  package Mylogger;
     'file_name', 'file_size', 'file_cksum',  
     'ver', 'cksum', 'ips',  use Fcntl ':flock'; # import LOCK_* constants
   ) {  #use YAML;
     die "No $_ specified in config file!"  use constant LOG_PRINT => 128;
       if (not exists $conf{$_});  use constant LOG_SAVE  =>  64;
   }  
   DESTROY {
   #print Dump \%conf;    my $self = shift;
   #exit;    if ($self->{'MYLOG'}) {
   return \%conf;      $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
 }      close $self->{'MYLOG'};
     }
 package Mylogger;  }
   
 use Fcntl ':flock'; # import LOCK_* constants  sub new {
 #use YAML;    my $package = shift;
 use constant LOG_PRINT => 128;    my $self = shift || {};
 use constant LOG_SAVE  =>  64;  
     $self->{'base_path'}  ||= '.';
 DESTROY {    $self->{'log_path'}   ||= $self->{'base_path'};
   my $self = shift;    $self->{'log_prefix'} ||= 'LOG';
   if ($self->{'MYLOG'}) {    $self->{'log_file'}   ||= GetLogName(
     $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");      $self->{'log_prefix'},
     close $self->{'MYLOG'};      $self->{'log_path'}
   }    );
 }    bless $self, $package;
   }
 sub new {  
   my $package = shift;  sub s
   my $self = shift || {};  {
     my $self = shift;
   $self->{'base_path'}  ||= '.';    my $m = shift;
   $self->{'log_path'}   ||= $self->{'base_path'};    return $self->mylog($m, LOG_SAVE);
   $self->{'log_prefix'} ||= 'LOG';  }
   $self->{'log_file'}   ||= GetLogName(  
     $self->{'log_prefix'},  sub p
     $self->{'log_path'}  {
   );    my $self = shift;
   bless $self, $package;    my $m = shift;
 }    return $self->mylog($m, LOG_PRINT);
   }
 sub s  
 {  sub sp
   my $self = shift;  {
   my $m = shift;    my $self = shift;
   return $self->mylog($m, LOG_SAVE);    my $m = shift;
 }    return $self->mylog($m, LOG_SAVE | LOG_PRINT);
   }
 sub p  
 {  sub mylog
   my $self = shift;  {
   my $m = shift;    my $self = shift;
   return $self->mylog($m, LOG_PRINT);  
 }    my $thing = shift;
     chomp $thing;
 sub sp  
 {    my $which = shift;
   my $self = shift;  
   my $m = shift;    my $MYLOG;
   return $self->mylog($m, LOG_SAVE | LOG_PRINT);    if ($which & LOG_PRINT) {
 }      print $thing, "\n";
     }
 sub mylog  
 {    if ($which & LOG_SAVE) {
   my $self = shift;      if ($self->{'MYLOG'}) {
         $MYLOG = $self->{'MYLOG'};
   my $thing = shift;      } else {
   chomp $thing;        unless ($MYLOG) {
                   open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
   my $which = shift;            or die "Couldn't open logfile!\n";
           my $ofh = select $MYLOG;
   my $MYLOG;          $|=1;
   if ($which & LOG_PRINT) {          select $ofh;
     print $thing, "\n";          $self->{'MYLOG'} = $MYLOG;
   }  
           $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
   if ($which & LOG_SAVE) {        }
     if ($self->{'MYLOG'}) {      }
       $MYLOG = $self->{'MYLOG'};      flock($MYLOG, LOCK_EX);
     } else {      print $MYLOG (scalar gmtime), "\t", $thing, "\n"
       unless ($MYLOG) {        or die "Couldn't print to MYLOG: $!";
                 open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})      flock($MYLOG, LOCK_UN);
           or die "Couldn't open logfile!\n";    }
         my $ofh = select $MYLOG;  }
         $|=1;  
         select $ofh;  sub GetLogName
         $self->{'MYLOG'} = $MYLOG;  {
     my $prefix  = shift || die "Invalid prefix passed for log";
         $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");  
       }    my $logdate = GetLogDate();
     }    my $logver  = 0;
     flock($MYLOG, LOCK_EX);    my $logname;
     print $MYLOG (scalar gmtime), "\t", $thing, "\n"  
       or die "Couldn't print to MYLOG: $!";    do {
     flock($MYLOG, LOCK_UN);      $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
   }      $logver++;
 }    } until (not -e $logname);
   
 sub GetLogName    return $logname;
 {  }
   my $prefix  = shift || die "Invalid prefix passed for log";  
   sub GetLogDate
   my $logdate = GetLogDate();  {
   my $logver  = 0;    my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
   my $logname;  
     $mon++;
   do {    $year += 1900;
     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';  
     $logver++;    if ($min  < 10) { $min  = "0$min"  }
   } until (not -e $logname);    if ($sec  < 10) { $sec  = "0$sec"  }
     if ($hour < 10) { $hour = "0$hour" }
   return $logname;    if ($mday < 10) { $mday = "0$mday" }
 }    if ($mon  < 10) { $mon  = "0$mon"  }
   
 sub GetLogDate    my $time = $year . $mon . $mday;
 {  
   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();    return $time;
   }
   $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.13  
changed lines
  Added in v.1.18

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