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

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

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