/[mdap]/mdap-server.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /mdap-server.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 20 by dpavlin, Mon Apr 23 19:40:57 2007 UTC revision 67 by dpavlin, Sun Nov 18 01:29:04 2007 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2    
3    use warnings;
4  use strict;  use strict;
5  use IO::Socket::Multicast;  use IO::Socket::Multicast;
6  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
7  use Getopt::Long;  use Getopt::Long;
8    
9    use lib './lib';
10    use MDAP;
11    
12  my $GROUP = '224.0.0.103';  my $GROUP = '224.0.0.103';
13  my $PORT  = '3235';  my $PORT  = '3235';
14    
15  my $debug = 0;  #my $debug = 0; # from MDAP
16  my $quiet = 1;  my $quiet = 1;
17  my $verbose = 0;  my $verbose = 0;
18    my $credentials = 0;
19    
20    my $plugins = 1;
21    
22  my $resend_search_delay = 3;  my $resend_search_delay = 3;
23  my $tftp_path = '/srv/tftp/';  my $tftp_path = '/srv/tftp/';
24    
25    my $flashed_cmd = 'system config led=flash';
26    
27  GetOptions(  GetOptions(
28          "port=i"        => \$PORT,          "port=i"        => \$PORT,
29          "group=s"       => \$GROUP,          "group=s"       => \$GROUP,
# Line 23  GetOptions( Line 32  GetOptions(
32          "verbose!"      => \$verbose,          "verbose!"      => \$verbose,
33          "search=i"      => \$resend_search_delay,          "search=i"      => \$resend_search_delay,
34          "tftp=s"        => \$tftp_path,          "tftp=s"        => \$tftp_path,
35            "credentials"   => \$credentials,
36            "exec=s"        => \$flashed_cmd,
37            'plugins!'      => \$plugins,
38  );  );
39    
40  $quiet = 0 if $verbose;  $quiet = 0 if $verbose;
41    $credentials = 1 if $debug;
42    
43  # tab-delimited list of user id/passwd to try on ants  # tab-delimited list of user id/passwd to try on ants
44  my @try_accounts = ( "Administrator\t" );  my @try_accounts = ( "Administrator\t" );
# Line 38  if (-e $passwd_path) { Line 51  if (-e $passwd_path) {
51          while(<$fh>) {          while(<$fh>) {
52                  chomp;                  chomp;
53                  next if /^#/ || /^$/ || /^\s+$/;                  next if /^#/ || /^$/ || /^\s+$/;
54                  if (/^\S+\t\S+$/) {                  if (/^\S+\s+\S+$/) {
55                          push @try_accounts, $_;                          push @try_accounts, $_;
56                  } else {                  } else {
57                          warn "invalid $passwd_path entry: $_\n";                          warn "invalid $passwd_path entry: $_\n";
58                  }                  }
59          }          }
60          print "found ", $#try_accounts + 1, " accounts to try on password protected ants\n";          print "found ", $#try_accounts + 1, " accounts to try on password protected ants",
61                    $credentials ? " and display credentials" : "", "\n";
62  }  }
63    
64  warn "search for ants every ${resend_search_delay}s\ntftp server path: $tftp_path\n";  warn "search for ants every ${resend_search_delay}s\ntftp server path: $tftp_path\nflashed to current version: $flashed_cmd\n";
65    
66  sub fw {  sub fw {
67          my ($board, $offset,$len) = @_;          my ($board, $offset,$len) = @_;
# Line 96  sub mdap_send { Line 110  sub mdap_send {
110    
111          $sock->mcast_send( $data, "${GROUP}:${PORT}" );          $sock->mcast_send( $data, "${GROUP}:${PORT}" );
112          if ($debug) {          if ($debug) {
113                  warn ">> ", dump( $data ), $/;                  warn ">> ${GROUP}:${PORT} >> ", dump( $data ), $/;
114          } elsif( ! $quiet ) {          } elsif( ! $quiet ) {
115                  $data =~ s/\s+/ /gi;                  $data =~ s/\s+/ /gi;
116                  warn ">> ", substr($data,0,70), $/;                  warn ">> ", substr($data,0,70), $/;
# Line 104  sub mdap_send { Line 118  sub mdap_send {
118  }  }
119    
120  my $ant_passwd;  my $ant_passwd;
121    my $ant_unknown_password;
122    my $ant_ok_password;
123    my $ant_flashing;
124    
125  sub ant_credentials {  sub ant_credentials {
126          my $ant = shift || die "no ant?";          my $ant = shift || die "no ant?";
# Line 113  sub ant_credentials { Line 130  sub ant_credentials {
130          return ($user_id,$user_pwd);          return ($user_id,$user_pwd);
131  }  }
132    
133  my $ant_password_protected;  sub ant_unknown_password {
134            my $ant = shift || die "no ant?";
135            if ( $ant_unknown_password->{$ant} ) {
136                    $ant_unknown_password->{$ant}--;
137            }
138            return $ant_unknown_password->{$ant};
139    }
140    
141  sub ant_another_passwd {  sub ant_another_passwd {
142          my $ant = shift || die "no ant?";          my $ant = shift || die "no ant?";
143          return if ( $ant_password_protected->{$ant} );  
144            return 0 if ant_unknown_password( $ant );
145            return 0 if $ant_ok_password->{$ant};
146    
147          $ant_passwd->{$ant}++;          $ant_passwd->{$ant}++;
148    
149          if ( $ant_passwd->{$ant} > $#try_accounts ) {          if ( $ant_passwd->{$ant} > $#try_accounts ) {
150                  print "!! ant $ant password protected\n";                  print "$ant ant with unknown password\n";
151                  $ant_password_protected->{$ant}++;                  $ant_unknown_password->{$ant} = 10;
152                    $ant_passwd->{$ant} = 0;
153                  return 0;                  return 0;
154          }          }
155          return 1;          return 1;
156  }  }
157    
158  my $once;  sub forget_ant {
159            my $ant = shift || die "no ant?";
160            delete $ant_unknown_password->{$ant};
161            delete $ant_passwd->{$ant};
162            delete $ant_ok_password->{$ant};
163    }
164    
165    my $status = '';
166    
167  sub once {  sub status {
168          my $m = join(" ", @_);          my $m = join('', @_);
169          $once->{$m}++;          if ($m ne $status) {
170          print $m if ($once->{$m} == 1);                  print $m;
171                    $status = $m;
172            }
173  }  }
174    
175  local $SIG{ALRM} = sub {  local $SIG{ALRM} = sub {
# Line 161  while (1) { Line 196  while (1) {
196    
197                  print "<< $type $proto/$mdap_ver << ", length($data), " bytes\n" unless $quiet;                  print "<< $type $proto/$mdap_ver << ", length($data), " bytes\n" unless $quiet;
198    
199                  warn dump($h),$/ if ($debug);                  warn dump( $data, $h ),$/ if ($debug);
200    
201                  # we are getting our own messages (since our source port                  # we are getting our own messages (since our source port
202                  # is same as destination)                  # is same as destination)
203                  next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );                  next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );
204    
205                  my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );                  my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );
206                    my $seq_nr = $1 if (defined $h->{'SEQ-NR'} && $h->{'SEQ-NR'} =~ m/^-*(\d)+/);
207                    #warn "SEQ-NR: $seq_nr ok: ",$ant_ok_password->{$ant},"\n" if ($seq_nr);
208    
209                  my ($user_id,$user_pwd) = ant_credentials( $ant );                  my ($user_id,$user_pwd) = ant_credentials( $ant );
210    
211                  if ($type eq 'REPLY-ANT-SEARCH') {                  if ($type eq 'REPLY-ANT-SEARCH') {
212                          mdap_send("INFO MDAP/$mdap_ver\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n") unless ( $ant_password_protected->{$ant} );                          mdap_send("INFO MDAP/$mdap_ver\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n") unless ant_unknown_password( $ant );
213                  } elsif ($type eq 'REPLY-INFO') {                  } elsif ($type eq 'REPLY-INFO') {
214    
215                          if ($h->{'SEQ-NR'} < 0) {                          if ( $seq_nr < 0 ) {
216    #                               if ( $ant_ok_password ) {
217    #                                       $ant_ok_password->{$ant} = 0;
218    #                               } elsif ( ant_another_passwd( $ant ) ) {
219                                  if ( ant_another_passwd( $ant ) ) {                                  if ( ant_another_passwd( $ant ) ) {
220                                          my ($user_id,$user_pwd) = ant_credentials( $ant );                                          ($user_id,$user_pwd) = ant_credentials( $ant );
221                                          mdap_send("INFO MDAP/$mdap_ver\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n");                                          mdap_send("INFO MDAP/$mdap_ver\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n");
222                                  }                                  }
223                                  next;                                  next;
224                            } else {
225                                    $ant_ok_password->{$ant}++;
226                                    print "$ant credentials $user_id $user_pwd\n" if ( $credentials && $ant_ok_password->{$ant} == 1 ) || $debug;
227                          }                          }
228    
229                          my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?";                          my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?";
# Line 188  while (1) { Line 231  while (1) {
231                                  my $build = $h->{'_BUILD'} || die "no _BUILD?";                                  my $build = $h->{'_BUILD'} || die "no _BUILD?";
232                                  my $new_build = fw_build( $board );                                  my $new_build = fw_build( $board );
233                                  if ( $build ne $new_build ) {                                  if ( $build ne $new_build ) {
234                                          print "UPDATE STEP 1 on ant $ant version $build -> $new_build\n";                                          print "+ $ant version $build -> $new_build\n";
235                                          mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nCLI-CMD:software upgrade\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n");                                          mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nCLI-CMD:software upgrade\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n");
236                                            $ant_flashing->{$ant}++;
237                                            next;
238                                  } else {                                  } else {
239                                          once "OK ant $ant has $build\n";                                          once "$ant OK version $build",
240                                                    $ant_unknown_password->{$ant} ? ' with unknown password' :
241                                                    $ant_passwd->{$ant} ? ' password protected' :
242                                                    '';
243    
244                                            $ant_flashing->{$ant} = 0;
245                                            # green|red|orange|flash|off
246                                            mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nCLI-CMD:$flashed_cmd\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n");
247                                            my $waiting = 0;
248                                            my $count = 0;
249                                            map {
250                                                    $waiting++ if ($ant_flashing->{$_});
251                                                    $count++;
252                                            } keys %$ant_flashing;
253                                            if ($waiting == 0) {
254                                                    status "ALL $count ANTS FLASHED to $build\n";
255                                                    $ant_flashing = undef;
256                                            } else {
257                                                    status "$waiting of $count ants still flasing upto $build\n";
258                                            }
259                                    }
260    
261                                    if ( $plugins ) {
262                                            foreach my $plugin ( MDAP->plugins ) {
263                                                    warn "## calling $plugin\n" if $debug;
264                                                    if ( my $command = $plugin->check( $h ) ) {
265    
266                                                            warn ">> $plugin $ant ",
267                                                                    $h->{'ANT-NAME'}, " ",
268                                                                    $h->{'_BUILD'}, " ",
269                                                                    "\t:$command\n";
270                    
271                                                            mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nCLI-CMD:$command\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n");
272                                                            last;
273                                                    }
274                                            }
275                                  }                                  }
276    
277                          } else {                          } else {
278                                  once "!! NO FIRMWARE for $board in $tftp_path for ant $ant, skipping update\n";                                  once "!! NO FIRMWARE for $board in $tftp_path for ant $ant, skipping update";
279                          }                          }
280                  } elsif ( $type eq 'REPLY-EXEC-CLI' && $h->{'SEQ-NR'} == 1 ) {  
281                                  print "UPDATE STEP 2 on ant $ant\n";                  } elsif ( $type eq 'REPLY-EXEC-CLI' ) {
282                            print "##<< $type\n$data\n" if $debug;
283                            if ( $seq_nr == 1 ) {
284                                  mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nSEQ-NR:2\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n");                                  mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nSEQ-NR:2\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n");
285                                    forget_ant( $ant ) if ( $ant_flashing->{$ant} );
286                            } elsif ( $seq_nr < 0 ) {
287                                    warn "EXEC-CLI failed: $data\n";
288                            }
289    
290                  } else {                  } else {
291                          print "!! reply ignored ", dump( $h ), $/;                          print "!! reply $type ignored ", dump( $h ), $/;
292                  }                  }
293    
294          } else {          } else {

Legend:
Removed from v.20  
changed lines
  Added in v.67

  ViewVC Help
Powered by ViewVC 1.1.26