[BACK]Return to update_trango.pl CVS log [TXT][DIR] Up to [local] / trango / Net-Telnet-Trango / scripts

Annotation of trango/Net-Telnet-Trango/scripts/update_trango.pl, Revision 1.12

1.1       andrew      1: #!/usr/bin/perl
1.12    ! andrew      2: # $RedRiver: update_trango.pl,v 1.11 2005/12/21 02:03:30 andrew Exp $
1.1       andrew      3: ########################################################################
                      4: # update_trango.pl *** Updates trango foxes with a new firmware
                      5: # 
                      6: # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org>
                      7: ########################################################################
                      8: use strict;
                      9: use warnings;
                     10: 
1.2       andrew     11: use Net::TFTP;
1.12    ! andrew     12: use YAML;
        !            13: use lib '.';
        !            14: use Net::Telnet::Trango;
1.2       andrew     15: 
1.4       andrew     16: my $config_file = shift || 'update_trango.conf';
1.9       andrew     17: my $max_tries = 3;
1.8       andrew     18: 
1.2       andrew     19: 
                     20: 
1.9       andrew     21: my $l = Mylogger->new( { log_prefix => 'UT' } );
1.4       andrew     22: 
                     23: 
1.9       andrew     24: $l->sp("Reading config file '$config_file'");
1.8       andrew     25: my $conf = read_conf($config_file);
                     26: 
1.9       andrew     27: $l->sp("  Hardware Type: $conf->{'type'}");
                     28: $l->sp("  File Name:     $conf->{'file_name'}");
                     29: $l->sp("  File Size:     $conf->{'file_size'}");
                     30: $l->sp("  File Checksum: $conf->{'file_cksum'}");
                     31: $l->sp("  FW Version:    $conf->{'ver'}");
                     32: $l->sp("  FW Checksum:   $conf->{'cksum'}");
                     33: $l->sp("");
1.4       andrew     34: 
1.2       andrew     35: 
                     36: 
                     37: 
                     38: 
                     39: 
1.3       andrew     40: foreach my $fox (@{ $conf->{'ips'} }) {
1.9       andrew     41:   $l->sp("Updating: $fox");
1.2       andrew     42: 
                     43:   ## Connect and login.
1.12    ! andrew     44:   my $t = new Net::Telnet::Trango ({
1.9       andrew     45:     Host    => $fox,
                     46:     Timeout => 5,
                     47:   });
                     48: 
                     49:   $l->p("Connecting to $fox");
                     50:   my ($type, $version) = $t->connect();
1.2       andrew     51: 
1.9       andrew     52:   unless (defined $type && defined $version) {
                     53:     $l->sp("Error connecting!");
1.6       andrew     54:     next;
                     55:   }
1.3       andrew     56: 
                     57:   if ($type ne $conf->{'type'}) {
1.9       andrew     58:     $l->sp("Wrong type of unit ('$type' should be '$conf->{'type'}')");
                     59:     $t->close;
1.3       andrew     60:     next;
                     61:   }
                     62: 
                     63:   if ($version eq $conf->{'ver'}) {
1.9       andrew     64:     $l->sp("Already up to date with firmware version '$version'");
                     65:     $t->close;
1.5       andrew     66:     next;
                     67:   }
1.2       andrew     68: 
1.9       andrew     69:   $l->p("Logging in");
                     70:   $t->login($conf->{'password'}) || die "Couldn't login: $!";
1.3       andrew     71: 
1.9       andrew     72:   $l->p("Sending commands");
1.2       andrew     73:   ## Send commands
1.12    ! andrew     74:   #print Dump $t->ver();
        !            75:   #print Dump $t->tftpd();
1.9       andrew     76:   if ( upload($t, $conf->{'file_name'}) ) {
                     77:     $l->p("Rebooting");
                     78:     $t->reboot;
1.2       andrew     79:   } else {
1.9       andrew     80:     $l->p("Exiting");
                     81:     $t->exit;
1.2       andrew     82:   }
1.12    ! andrew     83:   
1.9       andrew     84:   $t->close;
                     85:   $l->sp("");
1.2       andrew     86: }
                     87: 
                     88: sub upload
                     89: {
1.9       andrew     90:   my $t    = shift;
1.2       andrew     91:   my $file = shift;
1.3       andrew     92: 
1.9       andrew     93:   my $ver = $t->ver;
1.11      andrew     94:   $l->p("Current version '$ver->{'Firmware Version'}'");
1.2       andrew     95: 
                     96:   if (
1.3       andrew     97:     $ver->{'Firmware Version'}  eq $conf->{'ver'} && 
                     98:     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}
1.2       andrew     99:   ) {
1.9       andrew    100:     $l->sp("Already updated!");
1.2       andrew    101:     return 1;
                    102:   }
                    103: 
                    104:   my $try = 0;
                    105:   while (1) {
1.3       andrew    106:     if ($try >= $max_tries) {
1.9       andrew    107:       $l->sp("Couldn't update in $max_tries tries!");
1.3       andrew    108:       return undef;
                    109:     }
1.2       andrew    110:     $try++;
                    111: 
1.9       andrew    112:     #sysinfo($self->{'_host'});
1.3       andrew    113:     
1.9       andrew    114:     $l->p("Enabling TFTPd");
                    115:     $t->enable_tftpd || die "Couldn't enable tftpd";
1.2       andrew    116: 
1.9       andrew    117:     $l->p("Uploading file ($file)");
1.2       andrew    118:     # use tftp to push the file up
1.9       andrew    119:     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
1.3       andrew    120: 
                    121:     $tftp->put($file, $file) 
                    122:       or die "Error uploading: " . $tftp->error;
1.2       andrew    123: 
                    124:     # waitfor some sort of output
                    125:     # make sure it says 'Success.' otherwise error
1.9       andrew    126:     #print "LAST: " . $self->lastline;
                    127:     #my @lines = $self->getlines;
1.3       andrew    128:     #print Dump \@lines;
1.2       andrew    129:     
1.9       andrew    130:     $l->p("Checking upload ($conf->{'file_cksum'})");
                    131:     my $results = $t->tftpd;
1.2       andrew    132:     # check the 'File Length' against ???
1.3       andrew    133:     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
1.10      andrew    134:       $l->sp(
                    135:         "File checksum '" . $results->{'File Checksum'} .
                    136:         "does not match config file '" . $conf->{'file_cksum'} . "'!"
                    137:       );
1.3       andrew    138:       next;
1.5       andrew    139:     } 
1.10      andrew    140:     $l->p("File checksum matches . . . ");
1.3       andrew    141: 
                    142:     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
1.10      andrew    143:       $l->sp(
                    144:         "File length '" . $results->{'File Length'} .
                    145:         "does not match config file '" . $conf->{'file_size'} . " bytes'!"
                    146:       );
1.3       andrew    147:       next;
                    148:     }
1.10      andrew    149:     $l->p("File length matches . . . ");
1.2       andrew    150: 
1.3       andrew    151:     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
1.10      andrew    152:       $l->sp(
                    153:         "File name '" . $results->{'File Name'} .
                    154:         "' does not match config file '" . $conf->{'file_name'} . "'!"
                    155:       );
1.3       andrew    156:       next;
                    157:     }
1.10      andrew    158:     $l->p("File name  matches . . . ");
1.2       andrew    159: 
1.9       andrew    160:     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");
                    161:     unless ($results = $t->updateflash(
                    162:       $ver->{'Firmware Checksum'}, $conf->{'cksum'}
                    163:     ) ) {
                    164:       $l->sp("Couldn't update flash: $!");
                    165:       next;
                    166:     }
                    167: 
1.3       andrew    168:     unless (
                    169:       defined $results->{'Checksum'} && 
                    170:       $results->{'Checksum'} eq $conf->{'cksum'}
                    171:     ) {
1.9       andrew    172:       $l->sp("Saved checksum does not match config file!");
1.3       andrew    173:       next;
1.2       andrew    174:     }
1.9       andrew    175:     $l->p("Uploaded checksum ($results->{'Checksum'}) " . 
1.5       andrew    176:           "matches ($conf->{'cksum'})");
1.3       andrew    177:     
1.9       andrew    178:     $l->sp("Successfully updated!");
1.3       andrew    179:     return 1;
1.2       andrew    180:   }
                    181: }
                    182: 
1.9       andrew    183: sub read_conf
1.2       andrew    184: {
1.9       andrew    185:   my $file = shift;
                    186:   my %conf;
                    187:   my $in_ip_list = 0;
                    188:   open my $fh, $file or die "Couldn't open file $file: $!";
                    189:   while (<$fh>) {
                    190:     chomp;
                    191:     next if /^#/;
                    192:     next if /^$/;
                    193:     if ($in_ip_list) {
1.10      andrew    194:       s/\s+//g; # Whitespace is a no no
                    195: 
1.9       andrew    196:       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {
                    197:         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);
                    198:       } else {
                    199:         push @{ $conf{'ips'} }, $_;
                    200:       }
                    201:     } else {
                    202:       my ($key, $val) = split /\s+/, $_, 2;
                    203: 
                    204:       if (lc($key) eq 'ips') {
                    205:         $in_ip_list = 1;
                    206:         next;
                    207:       }
                    208: 
1.10      andrew    209:       $key =~ s/^\s+//;
                    210:       $key =~ s/\s+$//;
                    211:       $val =~ s/^\s+//;
                    212:       $val =~ s/\s+$//;
                    213: 
1.9       andrew    214:       $conf{ lc($key) } = $val;
                    215:     }
                    216:   }
                    217:   close $fh;
                    218: 
                    219:   #print Dump \%conf;
                    220:   foreach (
                    221:     'password', 
                    222:     'file_name', 'file_size', 'file_cksum', 
                    223:     'ver', 'cksum', 'ips',
                    224:   ) {
                    225:     die "No $_ specified in config file!"
                    226:       if (not exists $conf{$_});
                    227:   }
                    228: 
                    229:   #print Dump \%conf;
                    230:   #exit;
                    231:   return \%conf;
                    232: }
1.3       andrew    233: 
1.9       andrew    234: package Mylogger;
                    235: 
                    236: use Fcntl ':flock'; # import LOCK_* constants
                    237: #use YAML;
                    238: use constant LOG_PRINT => 128;
                    239: use constant LOG_SAVE  =>  64;
                    240: 
                    241: DESTROY {
                    242:   my $self = shift;
                    243:   if ($self->{'MYLOG'}) {
                    244:     $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
                    245:     close $self->{'MYLOG'};
1.3       andrew    246:   }
1.9       andrew    247: }
                    248: 
                    249: sub new {
                    250:   my $package = shift;
                    251:   my $self = shift || {};
                    252: 
                    253:   $self->{'base_path'}  ||= '.';
                    254:   $self->{'log_path'}   ||= $self->{'base_path'};
                    255:   $self->{'log_prefix'} ||= 'LOG';
                    256:   $self->{'log_file'}   ||= GetLogName(
                    257:     $self->{'log_prefix'}, 
                    258:     $self->{'log_path'}
                    259:   );
                    260:   bless $self, $package;
                    261: }
                    262: 
                    263: sub s
                    264: {
                    265:   my $self = shift;
                    266:   my $m = shift;
                    267:   return $self->mylog($m, LOG_SAVE);
                    268: }
1.3       andrew    269: 
1.9       andrew    270: sub p
                    271: {
                    272:   my $self = shift;
                    273:   my $m = shift;
                    274:   return $self->mylog($m, LOG_PRINT);
                    275: }
1.3       andrew    276: 
1.9       andrew    277: sub sp
                    278: {
                    279:   my $self = shift;
                    280:   my $m = shift;
                    281:   return $self->mylog($m, LOG_SAVE | LOG_PRINT);
1.4       andrew    282: }
                    283: 
                    284: sub mylog
                    285: {
1.9       andrew    286:   my $self = shift;
                    287: 
1.5       andrew    288:   my $thing = shift;
                    289:   chomp $thing;
                    290: 
1.9       andrew    291:   my $which = shift;
1.5       andrew    292: 
1.9       andrew    293:   my $MYLOG;
                    294:   if ($which & LOG_PRINT) {
                    295:     print $thing, "\n";
                    296:   }
                    297: 
                    298:   if ($which & LOG_SAVE) {
                    299:     if ($self->{'MYLOG'}) {
                    300:       $MYLOG = $self->{'MYLOG'};
                    301:     } else {
                    302:       unless ($MYLOG) {
                    303:                open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'}) 
                    304:           or die "Couldn't open logfile!\n";
                    305:         my $ofh = select $MYLOG;
                    306:         $|=1;
                    307:         select $ofh;
                    308:         $self->{'MYLOG'} = $MYLOG;
                    309: 
                    310:         $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
                    311:       }
1.5       andrew    312:     }
                    313:     flock($MYLOG, LOCK_EX);
                    314:     print $MYLOG (scalar gmtime), "\t", $thing, "\n" 
                    315:       or die "Couldn't print to MYLOG: $!";
                    316:     flock($MYLOG, LOCK_UN);
                    317:   }
1.4       andrew    318: }
                    319: 
                    320: sub GetLogName
                    321: {
                    322:   my $prefix  = shift || die "Invalid prefix passed for log";
                    323: 
                    324:   my $logdate = GetLogDate();
                    325:   my $logver  = 0;
                    326:   my $logname;
                    327: 
                    328:   do {
                    329:     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
                    330:     $logver++;
                    331:   } until (not -e $logname);
                    332: 
                    333:   return $logname;
                    334: }
                    335: 
                    336: sub GetLogDate
                    337: {
                    338:   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
                    339: 
                    340:   $mon++;
                    341:   $year += 1900;
                    342: 
                    343:   if ($min  < 10) { $min  = "0$min"  }
                    344:   if ($sec  < 10) { $sec  = "0$sec"  }
                    345:   if ($hour < 10) { $hour = "0$hour" }
                    346:   if ($mday < 10) { $mday = "0$mday" }
                    347:   if ($mon  < 10) { $mon  = "0$mon"  }
                    348: 
                    349:   my $time = $year . $mon . $mday;
                    350: 
                    351:   return $time;
1.3       andrew    352: }

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