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

1.1       andrew      1: #!/usr/bin/perl
1.2     ! andrew      2: # $RedRiver: update_trango.pl,v 1.1 2005/11/16 03:28:42 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: 
                     11: use Net::Telnet;
1.2     ! andrew     12: use Net::TFTP;
        !            13: use YAML;
        !            14: 
        !            15: my @Foxes = (
        !            16:   '10.100.3.8',
        !            17: #  '10.100.3.11',
        !            18: #  '10.100.3.12',
        !            19: #  '10.100.3.13',
        !            20: );
        !            21: 
        !            22: my $password = 'password';
        !            23: 
        !            24: my $new_file  = 'fsu53_2p0a2H0003D05101403.s19';
        !            25: my $new_ver   = 'FSU 1p06H0003D04111001';
        !            26: my $new_cksum = 'x1D28DCC4';
        !            27: 
        !            28: my $max_tries = 3;
        !            29: 
        !            30: 
        !            31: 
        !            32: 
        !            33: 
        !            34: 
        !            35: foreach my $fox (@Foxes) {
        !            36:   print "getting temp from Fox: $fox\n";
        !            37:   ## Connect and login.
        !            38:   my $host = new Net::Telnet (Timeout => 5,
        !            39:                               Prompt => '/#> *$/');
        !            40:   $host->open($fox);
        !            41:   $host->dump_log('dump.log');
        !            42: 
        !            43:   ## Login to remote host.
        !            44:   $host->waitfor(
        !            45:     -match => '/password: ?$/i',
        !            46:     -errmode => "return",
        !            47:   ) or die "problem connecting to host ($fox): ", $host->lastline;
        !            48:   $host->print($password);
        !            49:   $host->waitfor(
        !            50:     -match => $host->prompt,
        !            51:     -errmode => "return",
        !            52:   ) or die "login ($fox) failed: ", $host->lastline;
        !            53:   
        !            54: 
        !            55:   ## Send commands
        !            56:   if ( upload($host, $new_file) ) {
        !            57:     $host->send('reboot');
        !            58:   } else {
        !            59:     $host->send('exit');
        !            60:   }
        !            61:   $host->close;
        !            62: }
        !            63: 
        !            64: sub upload
        !            65: {
        !            66:   my $host = shift;
        !            67:   my $file = shift;
        !            68:   my $ver = get_ver($host);
        !            69: 
        !            70:   if (
        !            71:     $ver->{'Firmware Version'} eq $new_ver && 
        !            72:     $ver->{'Checksum'}         eq $new_cksum
        !            73:   ) {
        !            74:     print "Already updated!";
        !            75:     return 1;
        !            76:   }
        !            77: 
        !            78:   my $try = 0;
        !            79:   while (1) {
        !            80:     $try++;
        !            81: 
        !            82:     enable_tftpd($host) || die "Couldn't enable tftpd";
        !            83: 
        !            84:     # use tftp to push the file up
        !            85: 
        !            86:     # waitfor some sort of output
        !            87:     # make sure it says 'Success.' otherwise error
        !            88:     
        !            89:     my $results = check_tftpd($host);
        !            90:     # check the 'File Length' against ???
        !            91: 
        !            92:     # 'updateflash mainimage $old_cksum $new_cksum'
        !            93:     # OR
        !            94:     # 'save mainimage [current firmware checksum] [new firmware checksum]'
        !            95:     # waitfor a prompt, look at the end of the output for 'Success.' otherwise
        !            96:     # error.
        !            97:     # decode_lines to get 'Checksum' to see if it matches $new_cksum
        !            98:     
        !            99:     $ver = get_ver($host);
        !           100:     # check versions
        !           101:     
        !           102: 
        !           103:     if ($try >= $max_tries) {
        !           104:       warn "Couldn't update in $max_tries tries!";
        !           105:       return undef;
        !           106:     }
        !           107:   }
        !           108: }
        !           109: 
        !           110: sub get_ver
        !           111: {
        !           112:   my $host = shift;
        !           113:   return cmd($host, 'ver');
        !           114: }
        !           115: 
        !           116: sub enable_tftpd
        !           117: {
        !           118:   my $host = shift;
        !           119: 
        !           120:   my $vals = cmd($host, 'tftpd on');
        !           121: 
        !           122:   if ($vals->{'Tftpd'} eq 'listen') {
        !           123:     return 1;
        !           124:   } else {
        !           125:     return undef;
        !           126:   }
        !           127: }
        !           128: 
        !           129: sub check_tftpd
        !           130: {
        !           131:   my $host = shift;
        !           132:   return cmd($host, 'tftpd');
        !           133: }
        !           134: 
        !           135: sub cmd
        !           136: {
        !           137:   my $host   = shift;
        !           138:   my $string = shift;
        !           139: 
        !           140:   my @lines = $host->cmd($string);
        !           141: 
        !           142:   my $vals = decode_lines(@lines);
        !           143:   return $vals;
        !           144: }
        !           145: 
        !           146: sub decode_lines
        !           147: {
        !           148:   ### XXX ver has 2 Checksums.  one for FPGA and one for Firmware.  DOH!
        !           149:   my @lines = @_;
        !           150: 
        !           151:   my %conf;
        !           152: 
        !           153:   my $key = '';
        !           154:   my $val = '';
        !           155:   my $in_key = 0;
        !           156:   my $in_val = 0;
        !           157: 
        !           158:   foreach my $line (@lines) {
        !           159:     my @chars = split //, $line;
        !           160: 
        !           161:     foreach my $c (@chars) {
        !           162:       next if $c eq "\r";
        !           163:       next if $c eq "\n";
        !           164: 
        !           165:       if ($c eq '[') {
        !           166:         $in_key = 1;
        !           167:         $in_val = 0;
        !           168:         if ($key) {
        !           169:           $val =~ s/\s+$//;
        !           170:           $conf{$key} = $val;
        !           171:           $key = '';
        !           172:           $val = '';
        !           173:         }
        !           174: 
        !           175:       } elsif ($c eq ']') {
        !           176:         $in_val = 1;
        !           177:         $in_key = 0;
        !           178:         $c = shift @chars;
        !           179: 
        !           180:       } elsif ($in_key) {
        !           181:         $key .= $c;
        !           182: 
        !           183:       } elsif ($in_val) {
        !           184:         $val .= $c;
        !           185:       }
        !           186:     }
        !           187:   }
        !           188:   print Dump \%conf;
        !           189: 
        !           190:   if (%conf) {
        !           191:     return \%conf;
        !           192:   } else {
        !           193:     return \@lines;
        !           194:   }
        !           195: }
1.1       andrew    196: 

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