| version 1.3, 2005/11/16 20:48:48 | version 1.21, 2007/02/01 17:08:44 | 
|  |  | 
| #!/usr/bin/perl | #!/usr/bin/perl | 
| # $RedRiver: update_trango.pl,v 1.2 2005/11/16 05:26:37 andrew Exp $ | # $RedRiver: update_trango.pl,v 1.20 2007/01/31 22:19:07 mike Exp $ | 
| ######################################################################## | ######################################################################## | 
| # update_trango.pl *** Updates trango foxes with a new firmware | # update_trango.pl *** Updates trango foxes with a new firmware | 
| # | # | 
| # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org> | # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org> | 
| ######################################################################## | ######################################################################## | 
| use strict; | use strict; | 
| use warnings; | use warnings; | 
|  |  | 
| use Net::Telnet; | use Net::TFTP; | 
| use Net::TFTP; | use lib '.'; | 
| #use YAML; | use Net::Telnet::Trango; | 
|  |  | 
|  | my $config_file = shift || 'update_trango.conf'; | 
| my $config_file = shift || 'fox53.conf'; | my $max_tries = 3; | 
| my $conf = read_conf($config_file); |  | 
|  |  | 
| my $max_tries = 1; |  | 
|  | my $l = Mylogger->new( { log_prefix => 'UT' } ); | 
|  |  | 
|  |  | 
|  | $l->sp("Reading config file '$config_file'"); | 
|  | my $conf = read_conf($config_file); | 
|  |  | 
| foreach my $fox (@{ $conf->{'ips'} }) { | $l->sp("  Hardware Type: $conf->{'type'}"); | 
| print STDERR "Updating: $fox\n"; | $l->sp("  File Name:     $conf->{'file_name'}"); | 
|  | $l->sp("  File Size:     $conf->{'file_size'}"); | 
| ## Connect and login. | $l->sp("  File Checksum: $conf->{'file_cksum'}"); | 
| my $host = new Net::Telnet (Timeout => 5, | $l->sp("  FW Version:    $conf->{'ver'}"); | 
| Prompt => '/#> *$/'); | $l->sp("  FW Checksum:   $conf->{'cksum'}"); | 
| print "Connecting to $fox\n"; | $l->sp(""); | 
| $host->open($fox); |  | 
| $host->dump_log('dump.log'); |  | 
|  |  | 
| ## Login to remote host. |  | 
| $host->waitfor( |  | 
| -match => '/password: ?$/i', |  | 
| -errmode => "return", | foreach my $fox (@{ $conf->{'ips'} }) { | 
| ) or die "problem connecting to host ($fox): ", $host->lastline; | $l->sp("Updating: $fox"); | 
|  |  | 
| my $login_banner = $host->lastline; | ## Connect and login. | 
| my ($type, $version) = $login_banner =~ | my $t = new Net::Telnet::Trango ( | 
| /Welcome to Trango Broadband Wireless (\w+)-(.+)$/i; | Timeout => 5, | 
|  | Errmode => 'return', | 
| if ($type ne $conf->{'type'}) { | ) or die "Couldn't make new connection: $!"; | 
| print STDERR "Wrong type of unit ('$type' should be '$conf->{'type'}')\n"; | $l->p("Connecting to $fox"); | 
| $host->close; | unless ( $t->open($fox) ) { | 
| next; | $l->sp("Error connecting: $!"); | 
| } | next; | 
|  | } | 
| if ($version eq $conf->{'ver'}) { |  | 
| print STDERR "Already up to date with firmware version '$version'\n"; | my $host_type = $t->host_type; | 
| $host->close; |  | 
| next; | if (lc($conf->{'firmware_type'}) eq 'fpga') { | 
| } | $host_type =~ s/\s.*$//; | 
|  | } | 
| print "Logging in\n"; |  | 
| $host->print($conf->{'password'}); | if ($host_type ne $conf->{'type'}) { | 
| $host->waitfor( | $l->sp("Wrong type of unit ('" . $t->host_type . "' should be '$conf->{'type'}')"); | 
| -match => $host->prompt, | $t->close; | 
| -errmode => "return", | next; | 
| ) or die "login ($fox) failed: ", $host->lastline; | } | 
|  |  | 
|  | if ($t->firmware_version eq $conf->{'ver'}) { | 
| print "Sending commands\n"; | $l->sp("Already up to date with firmware version '" . $t->firmware_version . "'"); | 
| ## Send commands | $t->close; | 
| if ( upload($host, $fox, $conf->{'file_name'}) ) { | next; | 
| print "Rebooting\n"; | } | 
| $host->print("reboot\n"); |  | 
| $host->getline; | $l->p("Logging in"); | 
| } else { | $t->login($conf->{'password'}) || die "Couldn't login: $!"; | 
| print "Exiting\n"; |  | 
| $host->print("exit\n"); | $l->p("Sending commands"); | 
| $host->getline; | ## Send commands | 
| } | if ( upload($t, $conf->{'file_name'}) ) { | 
| $host->close; | $l->p("Rebooting"); | 
| } | $t->reboot; | 
|  | } else { | 
| sub upload | $l->p("Exiting"); | 
| { | $t->exit; | 
| my $host = shift; | } | 
| my $fox  = shift; |  | 
| my $file = shift; | $l->sp(""); | 
|  | } | 
| print "Getting current version\n"; |  | 
| my $ver = get_ver($host); | sub upload | 
|  | { | 
| if ( | my $t    = shift; | 
| $ver->{'Firmware Version'}  eq $conf->{'ver'} && | my $file = shift; | 
| $ver->{'Firmware Checksum'} eq $conf->{'cksum'} |  | 
| ) { | my $fw_type = 'Firmware'; | 
| print STDERR "Already updated!"; | if (uc($conf->{'firmware_type'}) eq 'FPGA') { | 
| return 1; | $fw_type = 'FPGA'; | 
| } | } | 
|  |  | 
| my $try = 0; | my $ver = $t->ver; | 
| while (1) { | $l->p("Current version '" . $ver->{$fw_type . ' Version'} . "'"); | 
| if ($try >= $max_tries) { |  | 
| print STDERR "Couldn't update in $max_tries tries!"; | if ( | 
| return undef; | $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} && | 
| } | $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'} | 
| $try++; | ) { | 
|  | $l->sp("Already updated!"); | 
| #sysinfo($host); | return 0; | 
|  | } | 
| print "Enabling TFTPd\n"; |  | 
| enable_tftpd($host) || die "Couldn't enable tftpd"; | my $try = 0; | 
|  | while (1) { | 
| print "Uploading file\n"; | if ($try >= $max_tries) { | 
| # use tftp to push the file up | $l->sp("Couldn't update in $max_tries tries!"); | 
| my $tftp = Net::TFTP->new($fox, Mode => 'octet'); | return undef; | 
|  | } | 
| $tftp->put($file, $file) | $try++; | 
| or die "Error uploading: " . $tftp->error; |  | 
|  | #sysinfo($self->{'_host'}); | 
| # waitfor some sort of output |  | 
| # make sure it says 'Success.' otherwise error | $l->p("Enabling TFTPd"); | 
| #print "LAST: " . $host->lastline; | $t->enable_tftpd || die "Couldn't enable tftpd"; | 
| #my @lines = $host->getlines; |  | 
| #print Dump \@lines; | $l->p("Uploading file ($file)"); | 
|  | # use tftp to push the file up | 
| print "Checking upload\n"; | my $tftp = Net::TFTP->new($t->Host, Mode => 'octet'); | 
| my $results = check_tftpd($host); |  | 
| # check the 'File Length' against ??? | unless ($tftp->put($file, $file)) { | 
| if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) { | print "Error uploading: " . $tftp->error; | 
| print STDERR "File checksum does not match config file!"; | next; | 
| next; | } | 
| } |  | 
|  | # waitfor some sort of output | 
| if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) { | # make sure it says 'Success.' otherwise error | 
| print STDERR "File length does not match config file!"; | #print "LAST: " . $self->lastline; | 
| next; | #my @lines = $self->getlines; | 
| } | #print Dump \@lines; | 
|  |  | 
| if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) { | $l->p("Checking upload ($conf->{'file_cksum'})"); | 
| print STDERR "File name does not match config file!"; | my $results = $t->tftpd; | 
| next; | # check the 'File Length' against ??? | 
| } | if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) { | 
|  | $l->sp( | 
| print "Updating flash\n"; | "File checksum '" . $results->{'File Checksum'} . | 
| $results = updateflash( | " does not match config file '" . $conf->{'file_cksum'} . "'!" | 
| $host, $ver->{'Firmware Checksum'}, $conf->{'cksum'} | ); | 
| ) or die "Couldn't update flash: " . $host->lastline; | next; | 
| unless ( | } | 
| defined $results->{'Checksum'} && | $l->p("File checksum matches . . . "); | 
| $results->{'Checksum'} eq $conf->{'cksum'} |  | 
| ) { | if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) { | 
| print STDERR "Saved checksum does not match config file!"; | $l->sp( | 
| next; | "File length '" . $results->{'File Length'} . | 
| } | "does not match config file '" . $conf->{'file_size'} . " bytes'!" | 
|  | ); | 
| print STDERR "Successfully updated!\n"; | next; | 
| return 1; | } | 
| } | $l->p("File length matches . . . "); | 
| } |  | 
|  | if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) { | 
| sub get_ver | $l->sp( | 
| { | "File name '" . $results->{'File Name'} . | 
| my $host = shift; | "' does not match config file '" . $conf->{'file_name'} . "'!" | 
| return cmd($host, 'ver'); | ); | 
| } | next; | 
|  | } | 
| sub enable_tftpd | $l->p("File name  matches . . . "); | 
| { |  | 
| my $host = shift; | my $image_type = 'mainimage'; | 
|  | if ($fw_type eq 'FPGA') { | 
| my $vals = cmd($host, 'tftpd on', 'Success.'); | $image_type = 'fpgaimage'; | 
|  | } | 
| if ($vals->{'Tftpd'} eq 'listen') { | $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')"); | 
| return $vals; | unless ($results = $t->updateflash( | 
| } else { | args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} . | 
| return undef; | ' '          . $conf->{'cksum'}, | 
| } | Timeout => 90, | 
| } | ) ) { | 
|  | $l->sp("Couldn't update flash: $!"); | 
| sub check_tftpd | next; | 
| { | } | 
| my $host = shift; |  | 
| return cmd($host, 'tftpd', 'Success.'); | unless ( | 
| } | defined $results->{'Checksum'} && | 
|  | $results->{'Checksum'} eq $conf->{'cksum'} | 
| sub sysinfo | ) { | 
| { | $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!"); | 
| my $host = shift; | next; | 
| return cmd($host, 'sysinfo', 'Success.'); | } | 
| } | $l->p("Uploaded checksum ($results->{'Checksum'}) " . | 
|  | "matches ($conf->{'cksum'})"); | 
| sub updateflash |  | 
| { | $l->sp("Successfully updated!"); | 
| my $host = shift; | return 1; | 
| my $old = shift; | } | 
| my $new = shift; | } | 
|  |  | 
| return undef unless $new; | sub read_conf | 
|  | { | 
| return cmd($host, "updateflash mainimage $old $new", 'Success.', 90); | my $file = shift; | 
| } | my %conf; | 
|  | my $in_ip_list = 0; | 
| sub cmd | open my $fh, $file or die "Couldn't open file $file: $!"; | 
| { | while (<$fh>) { | 
| my $host   = shift; | chomp; | 
| my $string = shift; | next if /^#/; | 
| my $expect_last = shift; | next if /^$/; | 
| my $timeout = shift || 5; | if ($in_ip_list) { | 
|  | s/\s+//g; # Whitespace is a no no | 
| my @lines = $host->cmd(String => $string, Timeout => $timeout); |  | 
|  | if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) { | 
| my $vals = decode_lines(@lines); | push @{ $conf{'ips'} }, $1 . $_ for ($2..$3); | 
|  | } else { | 
| my $last = $host->lastline; | push @{ $conf{'ips'} }, $_; | 
|  | } | 
| unless ($expect_last) { | } else { | 
| return $vals; | my ($key, $val) = split /\s+/, $_, 2; | 
| } |  | 
|  | if (lc($key) eq 'ips') { | 
| if ($last =~ /$expect_last$/) { | $in_ip_list = 1; | 
| return $vals; | next; | 
| } else { | } | 
| warn "Error with command ($string): $last"; |  | 
| return undef; | $key =~ s/^\s+//; | 
| } | $key =~ s/\s+$//; | 
| } | $val =~ s/^\s+//; | 
|  | $val =~ s/\s+$//; | 
| sub decode_lines |  | 
| { | $conf{ lc($key) } = $val; | 
| my @lines = @_; | } | 
|  | } | 
| my %conf; | close $fh; | 
|  |  | 
| my $key = ''; | #print Dump \%conf; | 
| my $val = ''; | foreach ( | 
| my $in_key = 0; | 'password', | 
| my $in_val = 0; | 'file_name', 'file_size', 'file_cksum', | 
|  | 'ver', 'cksum', 'ips', | 
| foreach my $line (@lines) { | ) { | 
| my @chars = split //, $line; | die "No $_ specified in config file!" | 
|  | if (not exists $conf{$_}); | 
| my $last_key = ''; | } | 
| foreach my $c (@chars) { |  | 
|  | #print Dump \%conf; | 
| if ($c eq '[' || $c eq "\r" || $c eq "\n") { | #exit; | 
| if ($c eq '[') { | return \%conf; | 
| $in_key = 1; | } | 
| $in_val = 0; |  | 
| } else { | package Mylogger; | 
| $in_key = 0; |  | 
| $in_val = 0; | use Fcntl ':flock'; # import LOCK_* constants | 
| } | #use YAML; | 
|  | use constant LOG_PRINT => 128; | 
| if ($key) { | use constant LOG_SAVE  =>  64; | 
| $key =~ s/^\s+//; |  | 
| $key =~ s/\s+$//; | DESTROY { | 
|  | my $self = shift; | 
| $val =~ s/^\s+//; | if ($self->{'MYLOG'}) { | 
| $val =~ s/\s+$//; | $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})"); | 
|  | close $self->{'MYLOG'}; | 
| if ($key eq 'Checksum' && $last_key) { | } | 
| # Special case for these bastids. | } | 
| my $new = $last_key; |  | 
| $new =~ s/\s+\S+$//; | sub new { | 
| $key = $new . " " . $key; | my $package = shift; | 
| } | my $self = shift || {}; | 
|  |  | 
| $last_key = $key; | $self->{'base_path'}  ||= '.'; | 
| $conf{$key} = $val; | $self->{'log_path'}   ||= $self->{'base_path'}; | 
| $key = ''; | $self->{'log_prefix'} ||= 'LOG'; | 
| $val = ''; | $self->{'log_file'}   ||= GetLogName( | 
| } | $self->{'log_prefix'}, | 
|  | $self->{'log_path'} | 
| } elsif ($c eq ']') { | ); | 
| $in_val = 1; | bless $self, $package; | 
| $in_key = 0; | } | 
| $c = shift @chars; |  | 
|  | sub s | 
| } elsif ($in_key) { | { | 
| $key .= $c; | my $self = shift; | 
|  | my $m = shift; | 
| } elsif ($in_val) { | return $self->mylog($m, LOG_SAVE); | 
| $val .= $c; | } | 
| } |  | 
| } | sub p | 
| } | { | 
| #print Dump \%conf; | my $self = shift; | 
|  | my $m = shift; | 
| if (%conf) { | return $self->mylog($m, LOG_PRINT); | 
| return \%conf; | } | 
| } else { |  | 
| return \@lines; | sub sp | 
| } | { | 
| } | my $self = shift; | 
|  | my $m = shift; | 
| sub read_conf | return $self->mylog($m, LOG_SAVE | LOG_PRINT); | 
| { | } | 
| my $file = shift; |  | 
| my %conf; | sub mylog | 
| my $in_ip_list = 0; | { | 
| open my $fh, $file or die "Couldn't open file $file: $!"; | my $self = shift; | 
| while (<$fh>) { |  | 
| chomp; | my $thing = shift; | 
| next if /^#/; | chomp $thing; | 
| next if /^$/; |  | 
| if ($in_ip_list) { | my $which = shift; | 
| push @{ $conf{'ips'} }, $_; |  | 
| } else { | my $MYLOG; | 
| my ($key, $val) = split /\s+/, $_, 2; | if ($which & LOG_PRINT) { | 
|  | print $thing, "\n"; | 
| if (lc($key) eq 'ips') { | } | 
| $in_ip_list = 1; |  | 
| next; | if ($which & LOG_SAVE) { | 
| } | if ($self->{'MYLOG'}) { | 
|  | $MYLOG = $self->{'MYLOG'}; | 
| $conf{ lc($key) } = $val; | } else { | 
| } | unless ($MYLOG) { | 
| } | open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'}) | 
| close $fh; | or die "Couldn't open logfile!\n"; | 
|  | my $ofh = select $MYLOG; | 
| #print Dump \%conf; | $|=1; | 
| foreach ( | select $ofh; | 
| 'password', | $self->{'MYLOG'} = $MYLOG; | 
| 'file_name', 'file_size', 'file_cksum', |  | 
| 'ver', 'cksum', 'ips', | $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})"); | 
| ) { | } | 
| die "No $_ specified in config file!" | } | 
| if (not exists $conf{$_}); | flock($MYLOG, LOCK_EX); | 
| } | print $MYLOG (scalar gmtime), "\t", $thing, "\n" | 
|  | or die "Couldn't print to MYLOG: $!"; | 
| return \%conf; | flock($MYLOG, LOCK_UN); | 
| } | } | 
|  | } | 
|  |  | 
|  | sub GetLogName | 
|  | { | 
|  | my $prefix  = shift || die "Invalid prefix passed for log"; | 
|  |  | 
|  | my $logdate = GetLogDate(); | 
|  | my $logver  = 0; | 
|  | my $logname; | 
|  |  | 
|  | do { | 
|  | $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log'; | 
|  | $logver++; | 
|  | } until (not -e $logname); | 
|  |  | 
|  | return $logname; | 
|  | } | 
|  |  | 
|  | sub GetLogDate | 
|  | { | 
|  | my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime(); | 
|  |  | 
|  | $mon++; | 
|  | $year += 1900; | 
|  |  | 
|  | if ($min  < 10) { $min  = "0$min"  } | 
|  | if ($sec  < 10) { $sec  = "0$sec"  } | 
|  | if ($hour < 10) { $hour = "0$hour" } | 
|  | if ($mday < 10) { $mday = "0$mday" } | 
|  | if ($mon  < 10) { $mon  = "0$mon"  } | 
|  |  | 
|  | my $time = $year . $mon . $mday; | 
|  |  | 
|  | return $time; | 
|  | } |