Annotation of trango/Net-Telnet-Trango/scripts/update_trango.pl, Revision 1.21
1.16 mike 1: #!/usr/bin/perl
1.21 ! mike 2: # $RedRiver: update_trango.pl,v 1.20 2007/01/31 22:19:07 mike Exp $
1.16 mike 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::TFTP;
12: use lib '.';
13: use Net::Telnet::Trango;
14:
15: my $config_file = shift || 'update_trango.conf';
16: my $max_tries = 3;
17:
18:
19:
20: my $l = Mylogger->new( { log_prefix => 'UT' } );
21:
22:
23: $l->sp("Reading config file '$config_file'");
24: my $conf = read_conf($config_file);
25:
26: $l->sp(" Hardware Type: $conf->{'type'}");
27: $l->sp(" File Name: $conf->{'file_name'}");
28: $l->sp(" File Size: $conf->{'file_size'}");
29: $l->sp(" File Checksum: $conf->{'file_cksum'}");
30: $l->sp(" FW Version: $conf->{'ver'}");
31: $l->sp(" FW Checksum: $conf->{'cksum'}");
32: $l->sp("");
33:
34:
35:
36:
37:
38:
39: foreach my $fox (@{ $conf->{'ips'} }) {
40: $l->sp("Updating: $fox");
41:
42: ## Connect and login.
43: my $t = new Net::Telnet::Trango (
44: Timeout => 5,
45: Errmode => 'return',
46: ) or die "Couldn't make new connection: $!";
47: $l->p("Connecting to $fox");
48: unless ( $t->open($fox) ) {
49: $l->sp("Error connecting: $!");
50: next;
51: }
52:
1.21 ! mike 53: my $host_type = $t->host_type;
! 54:
! 55: if (lc($conf->{'firmware_type'}) eq 'fpga') {
! 56: $host_type =~ s/\s.*$//;
! 57: }
! 58:
! 59: if ($host_type ne $conf->{'type'}) {
1.17 mike 60: $l->sp("Wrong type of unit ('" . $t->host_type . "' should be '$conf->{'type'}')");
1.16 mike 61: $t->close;
62: next;
63: }
64:
65: if ($t->firmware_version eq $conf->{'ver'}) {
1.18 mike 66: $l->sp("Already up to date with firmware version '" . $t->firmware_version . "'");
1.16 mike 67: $t->close;
68: next;
69: }
70:
71: $l->p("Logging in");
72: $t->login($conf->{'password'}) || die "Couldn't login: $!";
73:
74: $l->p("Sending commands");
75: ## Send commands
1.17 mike 76: if ( upload($t, $conf->{'file_name'}) ) {
77: $l->p("Rebooting");
78: $t->reboot;
79: } else {
1.16 mike 80: $l->p("Exiting");
81: $t->exit;
1.17 mike 82: }
1.16 mike 83:
84: $l->sp("");
85: }
86:
87: sub upload
88: {
89: my $t = shift;
90: my $file = shift;
91:
1.19 andrew 92: my $fw_type = 'Firmware';
93: if (uc($conf->{'firmware_type'}) eq 'FPGA') {
94: $fw_type = 'FPGA';
95: }
96:
1.16 mike 97: my $ver = $t->ver;
1.19 andrew 98: $l->p("Current version '" . $ver->{$fw_type . ' Version'} . "'");
1.16 mike 99:
100: if (
1.19 andrew 101: $ver->{$fw_type . ' Version'} eq $conf->{'ver'} &&
102: $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
1.16 mike 103: ) {
104: $l->sp("Already updated!");
1.21 ! mike 105: return 0;
1.16 mike 106: }
107:
108: my $try = 0;
109: while (1) {
110: if ($try >= $max_tries) {
111: $l->sp("Couldn't update in $max_tries tries!");
112: return undef;
113: }
114: $try++;
115:
116: #sysinfo($self->{'_host'});
117:
118: $l->p("Enabling TFTPd");
119: $t->enable_tftpd || die "Couldn't enable tftpd";
120:
121: $l->p("Uploading file ($file)");
122: # use tftp to push the file up
123: my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
124:
1.21 ! mike 125: unless ($tftp->put($file, $file)) {
! 126: print "Error uploading: " . $tftp->error;
! 127: next;
! 128: }
1.16 mike 129:
130: # waitfor some sort of output
131: # make sure it says 'Success.' otherwise error
132: #print "LAST: " . $self->lastline;
133: #my @lines = $self->getlines;
134: #print Dump \@lines;
135:
136: $l->p("Checking upload ($conf->{'file_cksum'})");
137: my $results = $t->tftpd;
138: # check the 'File Length' against ???
139: if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
140: $l->sp(
141: "File checksum '" . $results->{'File Checksum'} .
1.20 mike 142: " does not match config file '" . $conf->{'file_cksum'} . "'!"
1.16 mike 143: );
144: next;
145: }
146: $l->p("File checksum matches . . . ");
147:
148: if ($results->{'File Length'} !~ /^$conf->{'file_size'} bytes/) {
149: $l->sp(
150: "File length '" . $results->{'File Length'} .
151: "does not match config file '" . $conf->{'file_size'} . " bytes'!"
152: );
153: next;
154: }
155: $l->p("File length matches . . . ");
156:
157: if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
158: $l->sp(
159: "File name '" . $results->{'File Name'} .
160: "' does not match config file '" . $conf->{'file_name'} . "'!"
161: );
162: next;
163: }
164: $l->p("File name matches . . . ");
165:
1.19 andrew 166: my $image_type = 'mainimage';
167: if ($fw_type eq 'FPGA') {
168: $image_type = 'fpgaimage';
169: }
170: $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
1.16 mike 171: unless ($results = $t->updateflash(
1.19 andrew 172: args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
1.16 mike 173: ' ' . $conf->{'cksum'},
174: Timeout => 90,
175: ) ) {
176: $l->sp("Couldn't update flash: $!");
177: next;
178: }
179:
180: unless (
181: defined $results->{'Checksum'} &&
182: $results->{'Checksum'} eq $conf->{'cksum'}
183: ) {
1.17 mike 184: $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " . $conf->{'cksum'} . "!");
1.16 mike 185: next;
186: }
187: $l->p("Uploaded checksum ($results->{'Checksum'}) " .
188: "matches ($conf->{'cksum'})");
189:
190: $l->sp("Successfully updated!");
191: return 1;
192: }
193: }
194:
195: sub read_conf
196: {
197: my $file = shift;
198: my %conf;
199: my $in_ip_list = 0;
200: open my $fh, $file or die "Couldn't open file $file: $!";
201: while (<$fh>) {
202: chomp;
203: next if /^#/;
204: next if /^$/;
205: if ($in_ip_list) {
206: s/\s+//g; # Whitespace is a no no
207:
208: if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {
209: push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);
210: } else {
211: push @{ $conf{'ips'} }, $_;
212: }
213: } else {
214: my ($key, $val) = split /\s+/, $_, 2;
215:
216: if (lc($key) eq 'ips') {
217: $in_ip_list = 1;
218: next;
219: }
220:
221: $key =~ s/^\s+//;
222: $key =~ s/\s+$//;
223: $val =~ s/^\s+//;
224: $val =~ s/\s+$//;
225:
226: $conf{ lc($key) } = $val;
227: }
228: }
229: close $fh;
230:
231: #print Dump \%conf;
232: foreach (
233: 'password',
234: 'file_name', 'file_size', 'file_cksum',
235: 'ver', 'cksum', 'ips',
236: ) {
237: die "No $_ specified in config file!"
238: if (not exists $conf{$_});
239: }
240:
241: #print Dump \%conf;
242: #exit;
243: return \%conf;
244: }
245:
246: package Mylogger;
247:
248: use Fcntl ':flock'; # import LOCK_* constants
249: #use YAML;
250: use constant LOG_PRINT => 128;
251: use constant LOG_SAVE => 64;
252:
253: DESTROY {
254: my $self = shift;
255: if ($self->{'MYLOG'}) {
256: $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
257: close $self->{'MYLOG'};
258: }
259: }
260:
261: sub new {
262: my $package = shift;
263: my $self = shift || {};
264:
265: $self->{'base_path'} ||= '.';
266: $self->{'log_path'} ||= $self->{'base_path'};
267: $self->{'log_prefix'} ||= 'LOG';
268: $self->{'log_file'} ||= GetLogName(
269: $self->{'log_prefix'},
270: $self->{'log_path'}
271: );
272: bless $self, $package;
273: }
274:
275: sub s
276: {
277: my $self = shift;
278: my $m = shift;
279: return $self->mylog($m, LOG_SAVE);
280: }
281:
282: sub p
283: {
284: my $self = shift;
285: my $m = shift;
286: return $self->mylog($m, LOG_PRINT);
287: }
288:
289: sub sp
290: {
291: my $self = shift;
292: my $m = shift;
293: return $self->mylog($m, LOG_SAVE | LOG_PRINT);
294: }
295:
296: sub mylog
297: {
298: my $self = shift;
299:
300: my $thing = shift;
301: chomp $thing;
302:
303: my $which = shift;
304:
305: my $MYLOG;
306: if ($which & LOG_PRINT) {
307: print $thing, "\n";
308: }
309:
310: if ($which & LOG_SAVE) {
311: if ($self->{'MYLOG'}) {
312: $MYLOG = $self->{'MYLOG'};
313: } else {
314: unless ($MYLOG) {
315: open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
316: or die "Couldn't open logfile!\n";
317: my $ofh = select $MYLOG;
318: $|=1;
319: select $ofh;
320: $self->{'MYLOG'} = $MYLOG;
321:
322: $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
323: }
324: }
325: flock($MYLOG, LOCK_EX);
326: print $MYLOG (scalar gmtime), "\t", $thing, "\n"
327: or die "Couldn't print to MYLOG: $!";
328: flock($MYLOG, LOCK_UN);
329: }
330: }
331:
332: sub GetLogName
333: {
334: my $prefix = shift || die "Invalid prefix passed for log";
335:
336: my $logdate = GetLogDate();
337: my $logver = 0;
338: my $logname;
339:
340: do {
341: $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
342: $logver++;
343: } until (not -e $logname);
344:
345: return $logname;
346: }
347:
348: sub GetLogDate
349: {
350: my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
351:
352: $mon++;
353: $year += 1900;
354:
355: if ($min < 10) { $min = "0$min" }
356: if ($sec < 10) { $sec = "0$sec" }
357: if ($hour < 10) { $hour = "0$hour" }
358: if ($mday < 10) { $mday = "0$mday" }
359: if ($mon < 10) { $mon = "0$mon" }
360:
361: my $time = $year . $mon . $mday;
362:
363: return $time;
364: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>