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

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

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