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

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

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.18

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