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

1.1       andrew      1: #!/usr/bin/perl
1.9     ! andrew      2: # $RedRiver: update_trango.pl,v 1.8 2005/11/17 20:20:59 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;
                     12: 
1.4       andrew     13: my $config_file = shift || 'update_trango.conf';
1.9     ! andrew     14: my $max_tries = 3;
1.8       andrew     15: 
1.2       andrew     16: 
                     17: 
1.9     ! andrew     18: my $l = Mylogger->new( { log_prefix => 'UT' } );
1.4       andrew     19: 
                     20: 
1.9     ! andrew     21: $l->sp("Reading config file '$config_file'");
1.8       andrew     22: my $conf = read_conf($config_file);
                     23: 
1.9     ! andrew     24: $l->sp("  Hardware Type: $conf->{'type'}");
        !            25: $l->sp("  File Name:     $conf->{'file_name'}");
        !            26: $l->sp("  File Size:     $conf->{'file_size'}");
        !            27: $l->sp("  File Checksum: $conf->{'file_cksum'}");
        !            28: $l->sp("  FW Version:    $conf->{'ver'}");
        !            29: $l->sp("  FW Checksum:   $conf->{'cksum'}");
        !            30: $l->sp("");
1.4       andrew     31: 
1.2       andrew     32: 
                     33: 
                     34: 
                     35: 
                     36: 
1.3       andrew     37: foreach my $fox (@{ $conf->{'ips'} }) {
1.9     ! andrew     38:   $l->sp("Updating: $fox");
1.2       andrew     39: 
                     40:   ## Connect and login.
1.9     ! andrew     41:   my $t = new Trango::Telnet ({
        !            42:     Host    => $fox,
        !            43:     Timeout => 5,
        !            44:   });
        !            45: 
        !            46:   $l->p("Connecting to $fox");
        !            47:   my ($type, $version) = $t->connect();
1.2       andrew     48: 
1.9     ! andrew     49:   unless (defined $type && defined $version) {
        !            50:     $l->sp("Error connecting!");
1.6       andrew     51:     next;
                     52:   }
1.3       andrew     53: 
                     54:   if ($type ne $conf->{'type'}) {
1.9     ! andrew     55:     $l->sp("Wrong type of unit ('$type' should be '$conf->{'type'}')");
        !            56:     $t->close;
1.3       andrew     57:     next;
                     58:   }
                     59: 
                     60:   if ($version eq $conf->{'ver'}) {
1.9     ! andrew     61:     $l->sp("Already up to date with firmware version '$version'");
        !            62:     $t->close;
1.5       andrew     63:     next;
                     64:   }
1.2       andrew     65: 
1.9     ! andrew     66:   $l->p("Logging in");
        !            67:   $t->login($conf->{'password'}) || die "Couldn't login: $!";
1.3       andrew     68: 
1.9     ! andrew     69:   $l->p("Sending commands");
1.2       andrew     70:   ## Send commands
1.9     ! andrew     71:   if ( upload($t, $conf->{'file_name'}) ) {
        !            72:     $l->p("Rebooting");
        !            73:     $t->reboot;
1.2       andrew     74:   } else {
1.9     ! andrew     75:     $l->p("Exiting");
        !            76:     $t->exit;
1.2       andrew     77:   }
1.9     ! andrew     78:   $t->close;
        !            79:   $l->sp("");
1.2       andrew     80: }
                     81: 
                     82: sub upload
                     83: {
1.9     ! andrew     84:   my $t    = shift;
1.2       andrew     85:   my $file = shift;
1.3       andrew     86: 
1.9     ! andrew     87:   $l->p("Getting current version");
        !            88:   my $ver = $t->ver;
1.2       andrew     89: 
                     90:   if (
1.3       andrew     91:     $ver->{'Firmware Version'}  eq $conf->{'ver'} && 
                     92:     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}
1.2       andrew     93:   ) {
1.9     ! andrew     94:     $l->sp("Already updated!");
1.2       andrew     95:     return 1;
                     96:   }
                     97: 
                     98:   my $try = 0;
                     99:   while (1) {
1.3       andrew    100:     if ($try >= $max_tries) {
1.9     ! andrew    101:       $l->sp("Couldn't update in $max_tries tries!");
1.3       andrew    102:       return undef;
                    103:     }
1.2       andrew    104:     $try++;
                    105: 
1.9     ! andrew    106:     #sysinfo($self->{'_host'});
1.3       andrew    107:     
1.9     ! andrew    108:     $l->p("Enabling TFTPd");
        !           109:     $t->enable_tftpd || die "Couldn't enable tftpd";
1.2       andrew    110: 
1.9     ! andrew    111:     $l->p("Uploading file ($file)");
1.2       andrew    112:     # use tftp to push the file up
1.9     ! andrew    113:     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
1.3       andrew    114: 
                    115:     $tftp->put($file, $file) 
                    116:       or die "Error uploading: " . $tftp->error;
1.2       andrew    117: 
                    118:     # waitfor some sort of output
                    119:     # make sure it says 'Success.' otherwise error
1.9     ! andrew    120:     #print "LAST: " . $self->lastline;
        !           121:     #my @lines = $self->getlines;
1.3       andrew    122:     #print Dump \@lines;
1.2       andrew    123:     
1.9     ! andrew    124:     $l->p("Checking upload ($conf->{'file_cksum'})");
        !           125:     my $results = $t->tftpd;
1.2       andrew    126:     # check the 'File Length' against ???
1.3       andrew    127:     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
1.9     ! andrew    128:       $l->sp("File checksum does not match config file!");
1.3       andrew    129:       next;
1.5       andrew    130:     } 
1.9     ! andrew    131:     $l->p("File checksum ($results->{'File Checksum'}) " . 
1.5       andrew    132:           "matches ($conf->{'file_cksum'})");
1.3       andrew    133: 
                    134:     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
1.9     ! andrew    135:       $l->sp("File length does not match config file!");
1.3       andrew    136:       next;
                    137:     }
1.9     ! andrew    138:     $l->p("File length ($results->{'File Length'}) " . 
1.5       andrew    139:           "matches ($conf->{'file_size'})");
1.2       andrew    140: 
1.3       andrew    141:     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
1.9     ! andrew    142:       $l->sp("File name does not match config file!");
1.3       andrew    143:       next;
                    144:     }
1.9     ! andrew    145:     $l->p("File name ($results->{'File Name'}) " . 
1.5       andrew    146:           "matches ($conf->{'file_name'})");
1.2       andrew    147: 
1.9     ! andrew    148:     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");
        !           149:     unless ($results = $t->updateflash(
        !           150:       $ver->{'Firmware Checksum'}, $conf->{'cksum'}
        !           151:     ) ) {
        !           152:       $l->sp("Couldn't update flash: $!");
        !           153:       next;
        !           154:     }
        !           155: 
1.3       andrew    156:     unless (
                    157:       defined $results->{'Checksum'} && 
                    158:       $results->{'Checksum'} eq $conf->{'cksum'}
                    159:     ) {
1.9     ! andrew    160:       $l->sp("Saved checksum does not match config file!");
1.3       andrew    161:       next;
1.2       andrew    162:     }
1.9     ! andrew    163:     $l->p("Uploaded checksum ($results->{'Checksum'}) " . 
1.5       andrew    164:           "matches ($conf->{'cksum'})");
1.3       andrew    165:     
1.9     ! andrew    166:     $l->sp("Successfully updated!");
1.3       andrew    167:     return 1;
1.2       andrew    168:   }
                    169: }
                    170: 
1.9     ! andrew    171: sub read_conf
1.2       andrew    172: {
1.9     ! andrew    173:   my $file = shift;
        !           174:   my %conf;
        !           175:   my $in_ip_list = 0;
        !           176:   open my $fh, $file or die "Couldn't open file $file: $!";
        !           177:   while (<$fh>) {
        !           178:     chomp;
        !           179:     next if /^#/;
        !           180:     next if /^$/;
        !           181:     if ($in_ip_list) {
        !           182:       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {
        !           183:         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);
        !           184:       } else {
        !           185:         push @{ $conf{'ips'} }, $_;
        !           186:       }
        !           187:     } else {
        !           188:       my ($key, $val) = split /\s+/, $_, 2;
        !           189: 
        !           190:       if (lc($key) eq 'ips') {
        !           191:         $in_ip_list = 1;
        !           192:         next;
        !           193:       }
        !           194: 
        !           195:       $conf{ lc($key) } = $val;
        !           196:     }
        !           197:   }
        !           198:   close $fh;
        !           199: 
        !           200:   #print Dump \%conf;
        !           201:   foreach (
        !           202:     'password', 
        !           203:     'file_name', 'file_size', 'file_cksum', 
        !           204:     'ver', 'cksum', 'ips',
        !           205:   ) {
        !           206:     die "No $_ specified in config file!"
        !           207:       if (not exists $conf{$_});
        !           208:   }
        !           209: 
        !           210:   #print Dump \%conf;
        !           211:   #exit;
        !           212:   return \%conf;
        !           213: }
        !           214: 
        !           215: package Trango::Telnet;
        !           216: use base 'Net::Telnet';
        !           217: 
        !           218: my %PRIVATE = (
        !           219:   connected => 0,
        !           220:   logged_in => 0,
        !           221: );
        !           222: 
        !           223: sub new {
        !           224:   my $class = shift;
        !           225:   my $args = shift || {};
        !           226: 
        !           227:   $args->{'Timeout'} ||= 5;
        !           228:   $args->{'Prompt'}  ||= '/#> *$/';
        !           229: 
        !           230:   foreach my $key (keys %{ $args }) {
        !           231:     $PRIVATE{$key} = $args->{$key};
        !           232:   }
        !           233: 
        !           234:   my $self = $class->SUPER::new(%{ $args });
        !           235:   bless $self;
        !           236: 
        !           237:   #bless $self, $package;
        !           238:   return $self;
        !           239: }
        !           240: 
        !           241: sub connect
        !           242: {
        !           243:   my $self = shift;
        !           244: 
        !           245:   unless ( $self->open( 
        !           246:       Host => $PRIVATE{'Host'},
        !           247:       Errmode => 'return',
        !           248:   ) ) {
        !           249:     $! = "Couldn't connect to $self->{'Host'}.  Connection timed out.";
        !           250:     return undef, undef;
        !           251:   }
        !           252:   #$self->dump_log('dump.log');
        !           253: 
        !           254:   ## Login to remote host.
        !           255:   unless ($self->waitfor(
        !           256:     -match => '/password: ?$/i',
        !           257:     -errmode => "return",
        !           258:   ) ) {
        !           259:     $! = "problem connecting to host ($self->{'Host'}): " . $self->lastline;
        !           260:     return undef;
        !           261:   }
        !           262: 
        !           263:   $self->login_banner($self->lastline);
        !           264: 
        !           265:   $PRIVATE{'connected'} = 1;
        !           266: 
        !           267:   return ($self->host_type, $self->firmware_version);
1.2       andrew    268: }
                    269: 
1.9     ! andrew    270: sub login
1.2       andrew    271: {
1.9     ! andrew    272:   my $self = shift;
1.2       andrew    273: 
1.9     ! andrew    274:   my $password = shift;
1.2       andrew    275: 
1.9     ! andrew    276:   $self->print($password);
        !           277:   unless ($self->waitfor(
        !           278:     -match => $self->prompt,
        !           279:     -errmode => "return",
        !           280:   ) ) {
        !           281:     $! = "login ($self->{'Host'}) failed: " . $self->lastline;
1.2       andrew    282:     return undef;
                    283:   }
1.9     ! andrew    284: 
        !           285:   $PRIVATE{'logged_in'} = 1;
        !           286: 
        !           287:   return 1;
        !           288: }
        !           289: 
        !           290: sub login_banner
        !           291: {
        !           292:   my $self = shift;
        !           293: 
        !           294:   my $banner = shift || $PRIVATE{'login_banner'};
        !           295: 
        !           296:   my ($type, $ver) = $banner =~ 
        !           297:     /Welcome to Trango Broadband Wireless (\w+)-(.+)$/i;
        !           298: 
        !           299:   $self->host_type($type);
        !           300:   $self->firmware_version($ver); 
        !           301: 
        !           302:   return $banner;
        !           303: }
        !           304: 
        !           305: sub host_type 
        !           306: {
        !           307:   my $self = shift;
        !           308: 
        !           309:   my $type = shift || $PRIVATE{'host_type'};
        !           310:   $PRIVATE{'host_type'} = $type;
        !           311: 
        !           312:   return $type;
        !           313: }
        !           314: 
        !           315: sub firmware_version
        !           316: {
        !           317:   my $self = shift;
        !           318: 
        !           319:   my $ver = shift || $PRIVATE{'firmware_version'};
        !           320:   $PRIVATE{'firmware_version'} = $ver;
        !           321:   
        !           322:   return $ver
        !           323: }
        !           324: 
        !           325: sub Host
        !           326: {
        !           327:   my $self = shift;
        !           328:   
        !           329:   my $host = shift || $PRIVATE{'Host'};
        !           330:   $PRIVATE{'HOST'} = $host;
        !           331: 
        !           332:   return $host;
        !           333: }
        !           334: 
        !           335: 
        !           336: sub reboot
        !           337: {
        !           338:   my $self = shift;
        !           339: 
        !           340:   $self->print("reboot\n");
        !           341:   $self->getline;
        !           342: 
        !           343:   return 1;
        !           344: }
        !           345: 
        !           346: sub exit
        !           347: {
        !           348:   my $self = shift;
        !           349: 
        !           350:   $self->print("exit\n");
        !           351:   $self->getline;
        !           352: 
        !           353:   return 1;
1.2       andrew    354: }
                    355: 
1.9     ! andrew    356: sub ver
1.2       andrew    357: {
1.9     ! andrew    358:   my $self = shift;
        !           359: 
        !           360:   return $self->cmd('ver');
        !           361: }
        !           362: 
        !           363: sub tftpd
        !           364: {
        !           365:   my $self = shift;
        !           366: 
        !           367:   return $self->cmd('tftpd', 'Success.');
1.3       andrew    368: }
                    369: 
                    370: sub sysinfo
                    371: {
1.9     ! andrew    372:   my $self = shift;
        !           373: 
        !           374:   return $self->cmd('sysinfo', 'Success.');
        !           375: }
        !           376: 
        !           377: sub enable_tftpd
        !           378: {
        !           379:   my $self = shift;
        !           380: 
        !           381:   my $vals = $self->cmd('tftpd on', 'Success.');
        !           382: 
        !           383:   if ($vals->{'Tftpd'} eq 'listen') {
        !           384:     return $vals;
        !           385:   } else {
        !           386:     return undef;
        !           387:   }
1.3       andrew    388: }
                    389: 
                    390: sub updateflash
                    391: {
1.9     ! andrew    392:   my $self = shift;
        !           393: 
1.3       andrew    394:   my $old = shift;
                    395:   my $new = shift;
                    396: 
                    397:   return undef unless $new;
                    398: 
1.9     ! andrew    399:   return $self->cmd("updateflash mainimage $old $new", 'Success.', 90);
1.2       andrew    400: }
                    401: 
                    402: sub cmd
                    403: {
1.9     ! andrew    404:   my $self = shift;
        !           405: 
1.2       andrew    406:   my $string = shift;
1.3       andrew    407:   my $expect_last = shift;
1.9     ! andrew    408:   my $timeout = shift || $PRIVATE{'Timeout'};
1.2       andrew    409: 
1.9     ! andrew    410:   unless (defined $string) {
        !           411:     $! = "No command passed";
        !           412:     return undef;
        !           413:   }
1.2       andrew    414: 
1.9     ! andrew    415:   unless ($PRIVATE{'connected'}) {
        !           416:     $! = "Not connected";
        !           417:     return undef;
        !           418:   }
        !           419: 
        !           420:   unless ($PRIVATE{'logged_in'}) {
        !           421:     $! = "Not logged in";
        !           422:     return undef;
        !           423:   }
1.3       andrew    424: 
1.9     ! andrew    425:   my @lines = $self->SUPER::cmd(String => $string, Timeout => $timeout);
        !           426: 
        !           427:   my $vals = _decode_lines(@lines);
1.3       andrew    428: 
                    429:   unless ($expect_last) {
                    430:     return $vals;
                    431:   }
                    432: 
1.9     ! andrew    433:   my $last = $self->lastline;
        !           434: 
1.3       andrew    435:   if ($last =~ /$expect_last$/) {
                    436:     return $vals;
                    437:   } else {
                    438:     warn "Error with command ($string): $last";
                    439:     return undef;
                    440:   }
1.2       andrew    441: }
                    442: 
1.9     ! andrew    443: sub _decode_lines
1.2       andrew    444: {
                    445:   my @lines = @_;
                    446: 
                    447:   my %conf;
                    448: 
                    449:   my $key = '';
                    450:   my $val = '';
                    451:   my $in_key = 0;
                    452:   my $in_val = 0;
                    453: 
                    454:   foreach my $line (@lines) {
                    455:     my @chars = split //, $line;
                    456: 
1.3       andrew    457:     my $last_key = '';
1.2       andrew    458:     foreach my $c (@chars) {
                    459: 
1.3       andrew    460:       if ($c eq '[' || $c eq "\r" || $c eq "\n") {
                    461:         if ($c eq '[') {
                    462:           $in_key = 1;
                    463:           $in_val = 0;
                    464:         } else {
                    465:           $in_key = 0;
                    466:           $in_val = 0;
                    467:         }
                    468: 
1.2       andrew    469:         if ($key) {
1.3       andrew    470:           $key =~ s/^\s+//;
                    471:           $key =~ s/\s+$//;
                    472: 
                    473:           $val =~ s/^\s+//;
1.2       andrew    474:           $val =~ s/\s+$//;
1.3       andrew    475: 
                    476:           if ($key eq 'Checksum' && $last_key) {
                    477:             # Special case for these bastids.
                    478:             my $new = $last_key;
                    479:             $new =~ s/\s+\S+$//;
                    480:             $key = $new . " " . $key;
                    481:           }
                    482: 
                    483:           $last_key = $key;
1.2       andrew    484:           $conf{$key} = $val;
                    485:           $key = '';
                    486:           $val = '';
                    487:         }
                    488: 
                    489:       } elsif ($c eq ']') {
                    490:         $in_val = 1;
                    491:         $in_key = 0;
                    492:         $c = shift @chars;
                    493: 
                    494:       } elsif ($in_key) {
                    495:         $key .= $c;
                    496: 
                    497:       } elsif ($in_val) {
                    498:         $val .= $c;
                    499:       }
                    500:     }
                    501:   }
1.3       andrew    502:   #print Dump \%conf;
1.2       andrew    503: 
                    504:   if (%conf) {
                    505:     return \%conf;
                    506:   } else {
                    507:     return \@lines;
                    508:   }
                    509: }
1.1       andrew    510: 
1.3       andrew    511: 
                    512: 
1.9     ! andrew    513: package Mylogger;
        !           514: 
        !           515: use Fcntl ':flock'; # import LOCK_* constants
        !           516: #use YAML;
        !           517: use constant LOG_PRINT => 128;
        !           518: use constant LOG_SAVE  =>  64;
        !           519: 
        !           520: DESTROY {
        !           521:   my $self = shift;
        !           522:   if ($self->{'MYLOG'}) {
        !           523:     $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
        !           524:     close $self->{'MYLOG'};
1.3       andrew    525:   }
1.9     ! andrew    526: }
        !           527: 
        !           528: sub new {
        !           529:   my $package = shift;
        !           530:   my $self = shift || {};
        !           531: 
        !           532:   $self->{'base_path'}  ||= '.';
        !           533:   $self->{'log_path'}   ||= $self->{'base_path'};
        !           534:   $self->{'log_prefix'} ||= 'LOG';
        !           535:   $self->{'log_file'}   ||= GetLogName(
        !           536:     $self->{'log_prefix'}, 
        !           537:     $self->{'log_path'}
        !           538:   );
        !           539:   bless $self, $package;
        !           540: }
        !           541: 
        !           542: sub s
        !           543: {
        !           544:   my $self = shift;
        !           545:   my $m = shift;
        !           546:   return $self->mylog($m, LOG_SAVE);
        !           547: }
1.3       andrew    548: 
1.9     ! andrew    549: sub p
        !           550: {
        !           551:   my $self = shift;
        !           552:   my $m = shift;
        !           553:   return $self->mylog($m, LOG_PRINT);
        !           554: }
1.3       andrew    555: 
1.9     ! andrew    556: sub sp
        !           557: {
        !           558:   my $self = shift;
        !           559:   my $m = shift;
        !           560:   return $self->mylog($m, LOG_SAVE | LOG_PRINT);
1.4       andrew    561: }
                    562: 
                    563: sub mylog
                    564: {
1.9     ! andrew    565:   my $self = shift;
        !           566: 
1.5       andrew    567:   my $thing = shift;
                    568:   chomp $thing;
                    569: 
1.9     ! andrew    570:   my $which = shift;
1.5       andrew    571: 
1.9     ! andrew    572:   my $MYLOG;
        !           573:   if ($which & LOG_PRINT) {
        !           574:     print $thing, "\n";
        !           575:   }
        !           576: 
        !           577:   if ($which & LOG_SAVE) {
        !           578:     if ($self->{'MYLOG'}) {
        !           579:       $MYLOG = $self->{'MYLOG'};
        !           580:     } else {
        !           581:       unless ($MYLOG) {
        !           582:                open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'}) 
        !           583:           or die "Couldn't open logfile!\n";
        !           584:         my $ofh = select $MYLOG;
        !           585:         $|=1;
        !           586:         select $ofh;
        !           587:         $self->{'MYLOG'} = $MYLOG;
        !           588: 
        !           589:         $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
        !           590:       }
1.5       andrew    591:     }
                    592:     flock($MYLOG, LOCK_EX);
                    593:     print $MYLOG (scalar gmtime), "\t", $thing, "\n" 
                    594:       or die "Couldn't print to MYLOG: $!";
                    595:     flock($MYLOG, LOCK_UN);
                    596:   }
1.4       andrew    597: }
                    598: 
                    599: sub GetLogName
                    600: {
                    601:   my $prefix  = shift || die "Invalid prefix passed for log";
                    602: 
                    603:   my $logdate = GetLogDate();
                    604:   my $logver  = 0;
                    605:   my $logname;
                    606: 
                    607:   do {
                    608:     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
                    609:     $logver++;
                    610:   } until (not -e $logname);
                    611: 
                    612:   return $logname;
                    613: }
                    614: 
                    615: sub GetLogDate
                    616: {
                    617:   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
                    618: 
                    619:   $mon++;
                    620:   $year += 1900;
                    621: 
                    622:   if ($min  < 10) { $min  = "0$min"  }
                    623:   if ($sec  < 10) { $sec  = "0$sec"  }
                    624:   if ($hour < 10) { $hour = "0$hour" }
                    625:   if ($mday < 10) { $mday = "0$mday" }
                    626:   if ($mon  < 10) { $mon  = "0$mon"  }
                    627: 
                    628:   my $time = $year . $mon . $mday;
                    629: 
                    630:   return $time;
1.3       andrew    631: }

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