/[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 19 by dpavlin, Mon Apr 23 19:06:18 2007 UTC revision 47 by dpavlin, Fri Nov 16 14:59:30 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::ChangeIP;
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;
16  my $quiet = 1;  my $quiet = 1;
17  my $verbose = 0;  my $verbose = 0;
18    my $credentials = 0;
19    
20  my $resend_search_delay = 3;  my $resend_search_delay = 3;
21  my $tftp_path = '/srv/tftp/';  my $tftp_path = '/srv/tftp/';
22    
23    my $flashed_cmd = 'system config led=flash';
24    
25  GetOptions(  GetOptions(
26          "port=i"        => \$PORT,          "port=i"        => \$PORT,
27          "group=s"       => \$GROUP,          "group=s"       => \$GROUP,
# Line 23  GetOptions( Line 30  GetOptions(
30          "verbose!"      => \$verbose,          "verbose!"      => \$verbose,
31          "search=i"      => \$resend_search_delay,          "search=i"      => \$resend_search_delay,
32          "tftp=s"        => \$tftp_path,          "tftp=s"        => \$tftp_path,
33            "credentials"   => \$credentials,
34            "exec=s"        => \$flashed_cmd,
35  );  );
36    
37  $quiet = 0 if $verbose;  $quiet = 0 if $verbose;
38    $credentials = 1 if $debug;
39    
40  # tab-delimited list of user id/passwd to try on ants  # tab-delimited list of user id/passwd to try on ants
41  my @try_accounts = ( "Administrator\t" );  my @try_accounts = ( "Administrator\t" );
# Line 38  if (-e $passwd_path) { Line 48  if (-e $passwd_path) {
48          while(<$fh>) {          while(<$fh>) {
49                  chomp;                  chomp;
50                  next if /^#/ || /^$/ || /^\s+$/;                  next if /^#/ || /^$/ || /^\s+$/;
51                  if (/^\S+\t\S+$/) {                  if (/^\S+\s+\S+$/) {
52                          push @try_accounts, $_;                          push @try_accounts, $_;
53                  } else {                  } else {
54                          warn "invalid $passwd_path entry: $_\n";                          warn "invalid $passwd_path entry: $_\n";
55                  }                  }
56          }          }
57          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",
58                    $credentials ? " and display credentials" : "", "\n";
59  }  }
60    
61  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";
62    
63  sub fw {  sub fw {
64          my ($board, $offset,$len) = @_;          my ($board, $offset,$len) = @_;
# Line 96  sub mdap_send { Line 107  sub mdap_send {
107    
108          $sock->mcast_send( $data, "${GROUP}:${PORT}" );          $sock->mcast_send( $data, "${GROUP}:${PORT}" );
109          if ($debug) {          if ($debug) {
110                  warn ">> ", dump( $data ), $/;                  warn ">> ${GROUP}:${PORT} >> ", dump( $data ), $/;
111          } elsif( ! $quiet ) {          } elsif( ! $quiet ) {
112                  $data =~ s/\s+/ /gi;                  $data =~ s/\s+/ /gi;
113                  warn ">> ", substr($data,0,70), $/;                  warn ">> ", substr($data,0,70), $/;
114          }          }
115  }  }
116    
117  my $ant_passwd_try;  my $ant_passwd;
118    my $ant_unknown_password;
119    my $ant_ok_password;
120    my $ant_flashing;
121    
122  sub ant_credentials {  sub ant_credentials {
123          my $ant = shift || die "no ant?";          my $ant = shift || die "no ant?";
124          my $i = $ant_passwd_try->{$ant} || 0;          my $i = $ant_passwd->{$ant} || 0;
125          my ($user_id,$user_pwd) = split(/\t/, $try_accounts[$i]);          my ($user_id,$user_pwd) = split(/\t/, $try_accounts[$i]);
126          #warn "ant $ant as [$i] $user_id / $user_pwd\n";          #warn "ant $ant as [$i] $user_id / $user_pwd\n";
127          return ($user_id,$user_pwd);          return ($user_id,$user_pwd);
128  }  }
129    
130    sub ant_unknown_password {
131            my $ant = shift || die "no ant?";
132            if ( $ant_unknown_password->{$ant} ) {
133                    $ant_unknown_password->{$ant}--;
134            }
135            return $ant_unknown_password->{$ant};
136    }
137    
138  sub ant_another_passwd {  sub ant_another_passwd {
139          my $ant = shift || die "no ant?";          my $ant = shift || die "no ant?";
140          $ant_passwd_try->{$ant}++;  
141          $ant_passwd_try->{$ant} = 0 if ( $ant_passwd_try->{$ant} > $#try_accounts );          return 0 if ant_unknown_password( $ant );
142            return 0 if $ant_ok_password->{$ant};
143    
144            $ant_passwd->{$ant}++;
145    
146            if ( $ant_passwd->{$ant} > $#try_accounts ) {
147                    print "$ant ant with unknown password\n";
148                    $ant_unknown_password->{$ant} = 10;
149                    $ant_passwd->{$ant} = 0;
150                    return 0;
151            }
152            return 1;
153    }
154    
155    sub forget_ant {
156            my $ant = shift || die "no ant?";
157            delete $ant_unknown_password->{$ant};
158            delete $ant_passwd->{$ant};
159            delete $ant_ok_password->{$ant};
160    }
161    
162    my $once;
163    
164    sub once {
165            my $m = join('', @_);
166            $once->{$m}++;
167            print $m if ($once->{$m} == 1);
168    }
169    
170    my $status = '';
171    
172    sub status {
173            my $m = join('', @_);
174            if ($m ne $status) {
175                    print $m;
176                    $status = $m;
177            }
178  }  }
179    
180  local $SIG{ALRM} = sub {  local $SIG{ALRM} = sub {
# Line 143  while (1) { Line 201  while (1) {
201    
202                  print "<< $type $proto/$mdap_ver << ", length($data), " bytes\n" unless $quiet;                  print "<< $type $proto/$mdap_ver << ", length($data), " bytes\n" unless $quiet;
203    
204                  warn dump($h),$/ if ($debug);                  warn dump( $data, $h ),$/ if ($debug);
205    
206                  # we are getting our own messages (since our source port                  # we are getting our own messages (since our source port
207                  # is same as destination)                  # is same as destination)
208                  next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );                  next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );
209    
210                  my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );                  my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );
211                    my $seq_nr = $1 if (defined $h->{'SEQ-NR'} && $h->{'SEQ-NR'} =~ m/^-*(\d)+/);
212                    #warn "SEQ-NR: $seq_nr ok: ",$ant_ok_password->{$ant},"\n" if ($seq_nr);
213    
214                  my ($user_id,$user_pwd) = ant_credentials( $ant );                  my ($user_id,$user_pwd) = ant_credentials( $ant );
215    
216                  if ($type eq 'REPLY-ANT-SEARCH') {                  if ($type eq 'REPLY-ANT-SEARCH') {
217                          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") unless ant_unknown_password( $ant );
218                  } elsif ($type eq 'REPLY-INFO') {                  } elsif ($type eq 'REPLY-INFO') {
219    
220                          if ($h->{'SEQ-NR'} < 0) {                          if ( $seq_nr < 0 ) {
221                                  warn "!! password protected ant $ant, skipping\n";  #                               if ( $ant_ok_password ) {
222                                  ant_another_passwd( $ant );  #                                       $ant_ok_password->{$ant} = 0;
223    #                               } elsif ( ant_another_passwd( $ant ) ) {
224                                    if ( ant_another_passwd( $ant ) ) {
225                                            ($user_id,$user_pwd) = ant_credentials( $ant );
226                                            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");
227                                    }
228                                  next;                                  next;
229                            } else {
230                                    $ant_ok_password->{$ant}++;
231                                    print "$ant credentials $user_id $user_pwd\n" if ( $credentials && $ant_ok_password->{$ant} == 1 ) || $debug;
232                          }                          }
233    
234                          my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?";                          my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?";
# Line 168  while (1) { Line 236  while (1) {
236                                  my $build = $h->{'_BUILD'} || die "no _BUILD?";                                  my $build = $h->{'_BUILD'} || die "no _BUILD?";
237                                  my $new_build = fw_build( $board );                                  my $new_build = fw_build( $board );
238                                  if ( $build ne $new_build ) {                                  if ( $build ne $new_build ) {
239                                          print "UPDATE STEP 1 on ant $ant version $build -> $new_build\n";                                          print "+ $ant version $build -> $new_build\n";
240                                          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");
241                                            $ant_flashing->{$ant}++;
242                                  } else {                                  } else {
243                                          print "OK ant $ant allready updated...\n";                                          once "$ant OK version $build",
244                                                    $ant_unknown_password->{$ant} ? ' with unknown password' :
245                                                    $ant_passwd->{$ant} ? ' password protected' :
246                                                    '',
247                                                    "\n";
248    
249                                            $ant_flashing->{$ant} = 0;
250                                            # green|red|orange|flash|off
251                                            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");
252                                            my $waiting = 0;
253                                            my $count = 0;
254                                            map {
255                                                    $waiting++ if ($ant_flashing->{$_});
256                                                    $count++;
257                                            } keys %$ant_flashing;
258                                            if ($waiting == 0) {
259                                                    status "ALL $count ANTS FLASHED to $build\n";
260                                                    $ant_flashing = undef;
261                                            } else {
262                                                    status "$waiting of $count ants still flasing upto $build\n";
263                                            }
264                                  }                                  }
265                          } else {                          } else {
266                                  print "!! 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\n";
267                            }
268    
269                            if ( my $command = CWMP::ChangeIP::check( $h ) ) {
270                                    warn "## sending $command to $ant\n";
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                          }                          }
273                  } elsif ( $type eq 'REPLY-EXEC-CLI' && $h->{'SEQ-NR'} == 1 ) {  
274                                  print "UPDATE STEP 2 on ant $ant\n";                  } elsif ( $type eq 'REPLY-EXEC-CLI' ) {
275                            print "+ $type\n$data\n" if ($verbose);
276                            if ( $seq_nr == 1 ) {
277                                  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");
278                                    forget_ant( $ant ) if ( $ant_flashing->{$ant} );
279                            } elsif ( $seq_nr < 0 ) {
280                                    warn "EXEC-CLI failed\n";
281                            }
282    
283                  } else {                  } else {
284                          print "!! reply ignored ", dump( $h ), $/;                          print "!! reply $type ignored ", dump( $h ), $/;
285                  }                  }
286    
287          } else {          } else {

Legend:
Removed from v.19  
changed lines
  Added in v.47

  ViewVC Help
Powered by ViewVC 1.1.26