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

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

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