1 |
package CWMP::ChangeIP; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use Data::Dump qw/dump/; |
7 |
|
8 |
my $debug = 0; |
9 |
my $verbose = 1; |
10 |
|
11 |
my $serial2ip = { |
12 |
'CP0636JT3SH' => '192.168.1.242', |
13 |
'CP0644JTHJ4' => '192.168.1.243', |
14 |
'CP0624BH55U' => '192.168.1.244', |
15 |
}; |
16 |
|
17 |
sub check { |
18 |
my $h = shift; |
19 |
warn "## ",__PACKAGE__,"check(",dump($h),")" if $debug; |
20 |
|
21 |
my $serial = $h->{'_PROD_SERIAL_NBR'} || die "no serial?"; |
22 |
|
23 |
my $ip_list = $h->{'IP_LIST'} || die "no IP_LIST"; |
24 |
|
25 |
if ( my $ip = $serial2ip->{$serial} ) { |
26 |
return if $ip eq $ip_list; |
27 |
|
28 |
warn "## change_ip $serial ", |
29 |
$h->{'_VARIANT_FRIENDLY_NAME'}, " ", |
30 |
$h->{'_BOARD_NAME'}, " ", |
31 |
$h->{'_BUILD'}, " ", |
32 |
"IP $ip_list -> $ip\n" if $verbose; |
33 |
my $ip_status; |
34 |
$ip_status->{$_}-- foreach ( split(/;/, $ip_list ) ); |
35 |
$ip_status->{$_}++ foreach ( split(/;/, $ip ) ); |
36 |
foreach my $current_ip ( sort { $ip_status->{$b} <=> $ip_status->{$a} } keys %$ip_status ) { |
37 |
warn "## current_ip = $current_ip\n"; |
38 |
if ( $ip_status->{$current_ip} < 0 ) { |
39 |
warn "-- remove IP $current_ip\n"; |
40 |
return "ip ipdelete addr=$current_ip"; |
41 |
} elsif ( $ip_status->{$current_ip} > 0 ) { |
42 |
warn "-- add IP $current_ip\n"; |
43 |
return "ip ipadd intf=LocalNetwork addr=$current_ip"; |
44 |
} |
45 |
} |
46 |
return; |
47 |
} else { |
48 |
warn "!! can't find serial2ip mapping for $serial IP $ip_list\n"; |
49 |
return; |
50 |
} |
51 |
} |
52 |
|
53 |
|
54 |
|