| version 1.4, 2005/11/16 21:13:39 | version 1.26, 2007/02/02 18:08:56 | 
|  |  | 
| #!/usr/bin/perl | #!/usr/bin/perl | 
| # $RedRiver: update_trango.pl,v 1.3 2005/11/16 20:48:48 andrew Exp $ | # $RedRiver: update_trango.pl,v 1.25 2007/02/02 17:50:09 andrew Exp $ | 
| ######################################################################## | ######################################################################## | 
| # update_trango.pl *** Updates trango foxes with a new firmware | # update_trango.pl *** Updates trango hosts 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 YAML qw/ LoadFile /; | 
| use Net::TFTP; | use Net::TFTP; | 
| use Fcntl ':flock'; # import LOCK_* constants | use lib '.'; | 
| #use YAML; | use Net::Telnet::Trango; | 
|  | use RedRiver::Wireless; | 
|  |  | 
| my $config_file = shift || 'update_trango.conf'; | my $config_file = shift || 'update_trango.yaml'; | 
| my $conf = read_conf($config_file); | my $max_tries = 3; | 
|  |  | 
| my $max_tries = 1; | my $l = Mylogger->new( { log_prefix => 'UT' } ); | 
|  |  | 
| my $log_file =  GetLogName('UT'); | $l->sp("Reading config file '$config_file'"); | 
|  | my $conf = LoadFile($config_file); | 
|  |  | 
| my $MYLOG;  # file handle for logging so we don't have to close and open it all the time | $l->sp("Reading hosts"); | 
| END { | my $hosts = RedRiver::Wireless::Read_Hosts(); | 
| if ($MYLOG) { |  | 
| mylog("Closing log ($log_file)"); |  | 
| close $MYLOG; |  | 
| } | foreach my $host (@{ $hosts }) { | 
| } | next if $host->{group} !~ /^Trango/; | 
|  | $l->sp(""); | 
|  | $l->sp("Checking: $host->{name}"); | 
|  | my $needs_reboot = 0; | 
|  |  | 
|  | ## Connect and login. | 
|  | my $t = new Net::Telnet::Trango ( | 
|  | Timeout => 5, | 
|  | Errmode => 'return', | 
| foreach my $fox (@{ $conf->{'ips'} }) { | ) or die "Couldn't make new connection: $!"; | 
| mylog("Updating: $fox"); | $l->p("Connecting to $host->{name}"); | 
|  | unless ( $t->open($host->{name}) ) { | 
| ## Connect and login. | $l->sp("Error connecting: $!"); | 
| my $host = new Net::Telnet (Timeout => 5, | next; | 
| Prompt => '/#> *$/'); | } | 
| mylog("Connecting to $fox"); |  | 
| $host->open($fox); | my $password = $host->{Telnet_Password} || $conf->{general}->{password}; | 
| $host->dump_log('dump.log'); |  | 
|  | if ($host->{group} !~ /Client$/) { | 
| ## Login to remote host. | $l->p("Logging in"); | 
| $host->waitfor( | $t->login($password); | 
| -match => '/password: ?$/i', | unless ($t->logged_in) { | 
| -errmode => "return", | $l->p('Failed!'); | 
| ) or die "problem connecting to host ($fox): ", $host->lastline; | $t->close; | 
|  | next; | 
| my $login_banner = $host->lastline; | } | 
| my ($type, $version) = $login_banner =~ |  | 
| /Welcome to Trango Broadband Wireless (\w+)-(.+)$/i; | $l->sp("Getting sudb"); | 
|  | my $sudb = $t->sudb_view; | 
| if ($type ne $conf->{'type'}) { | if ($sudb) { | 
| mylog("Wrong type of unit ('$type' should be '$conf->{'type'}')"); | foreach my $su (@{ $sudb }) { | 
| $host->close; | $l->p("Getting su info $su->{suid}"); | 
| next; | my $su_info = $t->su_info( $su->{suid} ); | 
| } | if ($su_info->{ip}) { | 
|  | $l->sp("Adding host $su_info->{ip}"); | 
| if ($version eq $conf->{'ver'}) { | my $new_host = { | 
| mylog("Already up to date with firmware version '$version'"); | Telnet_Password => $host->{Telnet_Password}, | 
| $host->close; | group   => $host->{group} . '-Client', | 
| next; | name    => $su_info->{ip}, | 
| } | remarks => $su_info->{remarks}, | 
|  | }; | 
| mylog("Logging in"); | push @{ $hosts }, $new_host; | 
| $host->print($conf->{'password'}); | } else { | 
| $host->waitfor( | $l->sp("Couldn't get su info for $su->{suid}"); | 
| -match => $host->prompt, | if ($su_info->{ERR}) { | 
| -errmode => "return", | $l->sp("ERR: $su_info->{ERR}"); | 
| ) or die "login ($fox) failed: ", $host->lastline; | } | 
|  | next; | 
|  | } | 
| mylog("Sending commands"); | } | 
| ## Send commands | } | 
| if ( upload($host, $fox, $conf->{'file_name'}) ) { | } | 
| mylog("Rebooting"); |  | 
| $host->print("reboot\n"); | foreach my $firmware_type ('Firmware', 'FPGA') { | 
| $host->getline; |  | 
| } else { | if (! exists $conf->{$firmware_type}) { | 
| mylog("Exiting"); | $l->s("No configs for '$firmware_type'"); | 
| $host->print("exit\n"); | $t->close; | 
| $host->getline; | next; | 
| } | } | 
| $host->close; |  | 
| mylog(""); | my $host_type = $t->host_type; | 
| } | if ($firmware_type eq 'FPGA') { | 
|  | $host_type =~ s/\s.*$//; | 
| sub upload | } | 
| { |  | 
| my $host = shift; | if (! exists $conf->{$firmware_type}->{$host_type}) { | 
| my $fox  = shift; | $l->sp("No '$firmware_type' config for type $host_type"); | 
| my $file = shift; | $t->close; | 
|  | next; | 
| mylog("Getting current version"); | } | 
| my $ver = get_ver($host); |  | 
|  | if (! $t->logged_in) { | 
| if ( | $l->p("Logging in"); | 
| $ver->{'Firmware Version'}  eq $conf->{'ver'} && | $t->login($password); | 
| $ver->{'Firmware Checksum'} eq $conf->{'cksum'} | unless ($t->logged_in) { | 
| ) { | $l->p('Failed!'); | 
| mylog("Already updated!"); | $t->close; | 
| return 1; | next; | 
| } | } | 
|  | } | 
| my $try = 0; |  | 
| while (1) { | foreach my $k (keys %{ $conf->{general} }) { | 
| if ($try >= $max_tries) { | $conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k}; | 
| mylog("Couldn't update in $max_tries tries!"); | } | 
| return undef; | $conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type; | 
| } | $conf->{$firmware_type}->{$host_type}->{type} = $host_type; | 
| $try++; |  | 
|  | $l->sp("$host_type $firmware_type"); | 
| #sysinfo($host); | $l->p("Sending commands"); | 
|  | ## Send commands | 
| mylog("Enabling TFTPd"); | my $rc = upload($t, $conf->{$firmware_type}->{$host_type}); | 
| enable_tftpd($host) || die "Couldn't enable tftpd"; | if ($rc) { | 
|  | $l->sp("Successfull!"); | 
| mylog("Uploading file"); | $needs_reboot = 1; | 
| # use tftp to push the file up | } elsif (defined $rc) { | 
| my $tftp = Net::TFTP->new($fox, Mode => 'octet'); | $l->sp("Already up to date"); | 
|  | } else { | 
| $tftp->put($file, $file) | $l->sp("Failed"); | 
| or die "Error uploading: " . $tftp->error; | $t->bye; | 
|  | next; | 
| # waitfor some sort of output | } | 
| # make sure it says 'Success.' otherwise error |  | 
| #print "LAST: " . $host->lastline; | } | 
| #my @lines = $host->getlines; |  | 
| #print Dump \@lines; | if ($needs_reboot) { | 
|  | $l->sp("Rebooting $host->{name}"); | 
| mylog("Checking upload"); | $t->reboot; | 
| my $results = check_tftpd($host); | } else { | 
| # check the 'File Length' against ??? | $l->sp("Bye $host->{name}"); | 
| if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) { | $t->bye(); | 
| mylog("File checksum does not match config file!"); | } | 
| next; | } | 
| } |  | 
|  | sub upload | 
| if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) { | { | 
| mylog("File length does not match config file!"); | my $t    = shift; | 
| next; | my $conf = shift; | 
| } |  | 
|  | my $file = $conf->{firmware_path} . '/' . $conf->{file_name}; | 
| if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) { |  | 
| mylog("File name does not match config file!"); | my $fw_type = $conf->{firmware_type}; | 
| next; |  | 
| } | my $ver = $t->ver; | 
|  |  | 
| mylog("Updating flash"); | if (! ( | 
| $results = updateflash( | $ver->{$fw_type . ' Version'}  && | 
| $host, $ver->{'Firmware Checksum'}, $conf->{'cksum'} | $ver->{$fw_type . ' Checksum'} | 
| ) or die "Couldn't update flash: " . $host->lastline; | )) { | 
| unless ( | $l->sp("Error getting current version numbers"); | 
| defined $results->{'Checksum'} && | return; | 
| $results->{'Checksum'} eq $conf->{'cksum'} | } | 
| ) { |  | 
| mylog("Saved checksum does not match config file!"); | if ( | 
| next; | $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} && | 
| } | $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'} | 
|  | ) { | 
| mylog("Successfully updated!"); | return 0; | 
| return 1; | } | 
| } |  | 
| } | $l->sp("Config information:"); | 
|  | $l->sp("  Hardware Type: $conf->{'type'}"); | 
| sub get_ver | $l->sp("  File Name:     $conf->{'file_name'}"); | 
| { | $l->sp("  File Size:     $conf->{'file_size'}"); | 
| my $host = shift; | $l->sp("  File Checksum: $conf->{'file_cksum'}"); | 
| return cmd($host, 'ver'); | $l->sp("  Conf Version:  $conf->{'ver'}"); | 
| } | $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}"); | 
|  | $l->sp("  Conf Checksum: $conf->{'cksum'}"); | 
| sub enable_tftpd | $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}"); | 
| { |  | 
| my $host = shift; | $l->sp("Updating"); | 
|  | my $try = 0; | 
| my $vals = cmd($host, 'tftpd on', 'Success.'); | while (1) { | 
|  | if ($try >= $max_tries) { | 
| if ($vals->{'Tftpd'} eq 'listen') { | $l->sp("Couldn't update in $max_tries tries!"); | 
| return $vals; | return; | 
| } else { | } | 
| return undef; | $try++; | 
| } |  | 
| } | #sysinfo($self->{'_host'}); | 
|  |  | 
| sub check_tftpd | $l->p("Enabling TFTPd"); | 
| { | unless ($t->enable_tftpd) { | 
| my $host = shift; | $l->sp("Couldn't enable tftpd"); | 
| return cmd($host, 'tftpd', 'Success.'); | next; | 
| } | } | 
|  |  | 
| sub sysinfo | $l->p("Uploading file ($file)"); | 
| { | # use tftp to push the file up | 
| my $host = shift; | my $tftp = Net::TFTP->new($t->Host, Mode => 'octet'); | 
| return cmd($host, 'sysinfo', 'Success.'); |  | 
| } | unless ($tftp->put($file, $file)) { | 
|  | $l->sp("Error uploading: " . $tftp->error); | 
| sub updateflash | next; | 
| { | } | 
| my $host = shift; |  | 
| my $old = shift; | $l->p("Checking upload ($conf->{'file_cksum'})"); | 
| my $new = shift; | my $results = $t->tftpd; | 
|  | # check the 'File Length' against ??? | 
| return undef unless $new; | if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) { | 
|  | $l->sp( | 
| return cmd($host, "updateflash mainimage $old $new", 'Success.', 90); | "File checksum '" . $results->{'File Checksum'} . | 
| } | " does not match config file '" . $conf->{'file_cksum'} . "'!" | 
|  | ); | 
| sub cmd | next; | 
| { | } | 
| my $host   = shift; | $l->p("File checksum matches . . . "); | 
| my $string = shift; |  | 
| my $expect_last = shift; | if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) { | 
| my $timeout = shift || 5; | $l->sp( | 
|  | "File length '" . $results->{'File Length'} . | 
| my @lines = $host->cmd(String => $string, Timeout => $timeout); | "does not match config file '" . $conf->{'file_size'} . " bytes'!" | 
|  | ); | 
| my $vals = decode_lines(@lines); | next; | 
|  | } | 
| my $last = $host->lastline; | $l->p("File length matches . . . "); | 
|  |  | 
| unless ($expect_last) { | if ( uc($results->{'File Name'}) ne uc($file) ) { | 
| return $vals; | $l->sp( | 
| } | "File name '" . $results->{'File Name'} . | 
|  | "' does not match config file '" . $file . "'!" | 
| if ($last =~ /$expect_last$/) { | ); | 
| return $vals; | next; | 
| } else { | } | 
| warn "Error with command ($string): $last"; | $l->p("File name  matches . . . "); | 
| return undef; |  | 
| } | my $image_type = 'mainimage'; | 
| } | if ($fw_type eq 'FPGA') { | 
|  | $image_type = 'fpgaimage'; | 
| sub decode_lines | } | 
| { | $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')"); | 
| my @lines = @_; | unless ($results = $t->updateflash( | 
|  | args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} . | 
| my %conf; | ' '          . $conf->{'cksum'}, | 
|  | Timeout => 90, | 
| my $key = ''; | ) ) { | 
| my $val = ''; | $l->sp("Couldn't update flash: $!"); | 
| my $in_key = 0; | next; | 
| my $in_val = 0; | } | 
|  |  | 
| foreach my $line (@lines) { | unless ( | 
| my @chars = split //, $line; | defined $results->{'Checksum'} && | 
|  | $results->{'Checksum'} eq $conf->{'cksum'} | 
| my $last_key = ''; | ) { | 
| foreach my $c (@chars) { | $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!"); | 
|  | next; | 
| if ($c eq '[' || $c eq "\r" || $c eq "\n") { | } | 
| if ($c eq '[') { | $l->p("Uploaded checksum ($results->{'Checksum'}) " . | 
| $in_key = 1; | "matches ($conf->{'cksum'})"); | 
| $in_val = 0; |  | 
| } else { | $l->sp("Successfully updated!"); | 
| $in_key = 0; | return 1; | 
| $in_val = 0; | } | 
| } | } | 
|  |  | 
| if ($key) { | package Mylogger; | 
| $key =~ s/^\s+//; |  | 
| $key =~ s/\s+$//; | use Fcntl ':flock'; # import LOCK_* constants | 
|  | #use YAML; | 
| $val =~ s/^\s+//; | use constant LOG_PRINT => 128; | 
| $val =~ s/\s+$//; | use constant LOG_SAVE  =>  64; | 
|  |  | 
| if ($key eq 'Checksum' && $last_key) { | DESTROY { | 
| # Special case for these bastids. | my $self = shift; | 
| my $new = $last_key; | if ($self->{'MYLOG'}) { | 
| $new =~ s/\s+\S+$//; | $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})"); | 
| $key = $new . " " . $key; | close $self->{'MYLOG'}; | 
| } | } | 
|  | } | 
| $last_key = $key; |  | 
| $conf{$key} = $val; | sub new { | 
| $key = ''; | my $package = shift; | 
| $val = ''; | my $self = shift || {}; | 
| } |  | 
|  | $self->{'base_path'}  ||= '.'; | 
| } elsif ($c eq ']') { | $self->{'log_path'}   ||= $self->{'base_path'}; | 
| $in_val = 1; | $self->{'log_prefix'} ||= 'LOG'; | 
| $in_key = 0; | $self->{'log_file'}   ||= GetLogName( | 
| $c = shift @chars; | $self->{'log_prefix'}, | 
|  | $self->{'log_path'} | 
| } elsif ($in_key) { | ); | 
| $key .= $c; | bless $self, $package; | 
|  | } | 
| } elsif ($in_val) { |  | 
| $val .= $c; | sub s | 
| } | { | 
| } | my $self = shift; | 
| } | my $m = shift; | 
| #print Dump \%conf; | return $self->mylog($m, LOG_SAVE); | 
|  | } | 
| if (%conf) { |  | 
| return \%conf; | sub p | 
| } else { | { | 
| return \@lines; | my $self = shift; | 
| } | my $m = shift; | 
| } | return $self->mylog($m, LOG_PRINT); | 
|  | } | 
| sub read_conf |  | 
| { | sub sp | 
| my $file = shift; | { | 
| my %conf; | my $self = shift; | 
| my $in_ip_list = 0; | my $m = shift; | 
| open my $fh, $file or die "Couldn't open file $file: $!"; | return $self->mylog($m, LOG_SAVE | LOG_PRINT); | 
| while (<$fh>) { | } | 
| chomp; |  | 
| next if /^#/; | sub mylog | 
| next if /^$/; | { | 
| if ($in_ip_list) { | my $self = shift; | 
| push @{ $conf{'ips'} }, $_; |  | 
| } else { | my $thing = shift; | 
| my ($key, $val) = split /\s+/, $_, 2; | chomp $thing; | 
|  |  | 
| if (lc($key) eq 'ips') { | my $which = shift; | 
| $in_ip_list = 1; |  | 
| next; | my $MYLOG; | 
| } | if ($which & LOG_PRINT) { | 
|  | print $thing, "\n"; | 
| $conf{ lc($key) } = $val; | } | 
| } |  | 
| } | if ($which & LOG_SAVE) { | 
| close $fh; | if ($self->{'MYLOG'}) { | 
|  | $MYLOG = $self->{'MYLOG'}; | 
| #print Dump \%conf; | } else { | 
| foreach ( | unless ($MYLOG) { | 
| 'password', | open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'}) | 
| 'file_name', 'file_size', 'file_cksum', | or die "Couldn't open logfile!\n"; | 
| 'ver', 'cksum', 'ips', | my $ofh = select $MYLOG; | 
| ) { | $|=1; | 
| die "No $_ specified in config file!" | select $ofh; | 
| if (not exists $conf{$_}); | $self->{'MYLOG'} = $MYLOG; | 
| } |  | 
|  | $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})"); | 
| return \%conf; | } | 
| } | } | 
|  | flock($MYLOG, LOCK_EX); | 
| sub mylog | print $MYLOG (scalar gmtime), "\t", $thing, "\n" | 
| { | or die "Couldn't print to MYLOG: $!"; | 
| my $thing = shift; | flock($MYLOG, LOCK_UN); | 
| chomp $thing; | } | 
|  | } | 
| unless ($MYLOG) { |  | 
| open ($MYLOG, '>>', $log_file) | sub GetLogName | 
| or die "Couldn't open logfile!\n"; | { | 
| my $ofh = select $MYLOG; | my $prefix  = shift || die "Invalid prefix passed for log"; | 
| $|=1; |  | 
| select $ofh; | my $logdate = GetLogDate(); | 
| mylog("Opened log ($log_file)"); | my $logver  = 0; | 
| } | my $logname; | 
|  |  | 
| print $thing, "\n"; | do { | 
| flock($MYLOG, LOCK_EX); | $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log'; | 
| print $MYLOG (scalar gmtime), "\t", $thing, "\n" | $logver++; | 
| or die "Couldn't print to MYLOG: $!"; | } until (not -e $logname); | 
| flock($MYLOG, LOCK_UN); |  | 
| } | return $logname; | 
|  | } | 
| sub GetLogName |  | 
| { | sub GetLogDate | 
| my $prefix  = shift || die "Invalid prefix passed for log"; | { | 
|  | my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime(); | 
| my $logdate = GetLogDate(); |  | 
| my $logver  = 0; | $mon++; | 
| my $logname; | $year += 1900; | 
|  |  | 
| do { | if ($min  < 10) { $min  = "0$min"  } | 
| $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log'; | if ($sec  < 10) { $sec  = "0$sec"  } | 
| $logver++; | if ($hour < 10) { $hour = "0$hour" } | 
| } until (not -e $logname); | if ($mday < 10) { $mday = "0$mday" } | 
|  | if ($mon  < 10) { $mon  = "0$mon"  } | 
| return $logname; |  | 
| } | my $time = $year . $mon . $mday; | 
|  |  | 
| sub GetLogDate | return $time; | 
| { | } | 
| 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; |  | 
| } |  |