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