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

Annotation of /lib/MDAP/ChangeIP.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 85 - (hide annotations)
Wed Dec 5 23:13:38 2007 UTC (11 years, 10 months ago) by dpavlin
File size: 1567 byte(s)
use NetAddr::IP to assign IP addresses to unknown ants
1 dpavlin 49 package MDAP::ChangeIP;
2 dpavlin 47
3     use strict;
4     use warnings;
5    
6 dpavlin 49 use Module::Pluggable;
7 dpavlin 58 use MDAP;
8 dpavlin 85 use NetAddr::IP;
9 dpavlin 47
10 dpavlin 85 # start IPs
11     my $ip1 = NetAddr::IP->new('192.168.1.242/24');
12     my $ip2 = NetAddr::IP->new('10.0.0.1/8');
13 dpavlin 47
14 dpavlin 85 my @serials = ( qw/
15     CP0636JT3SH
16     CP0644JTHJ4
17     CP0624BH55U
18     CP0703KT3PP
19     /);
20    
21     my $serial2ip;
22    
23     foreach my $serial ( @serials ) {
24     $serial2ip->{$serial} = $ip1++ . ';' . $ip2++;
25     }
26    
27 dpavlin 47 sub check {
28 dpavlin 49 my ( $self, $h ) = @_;
29 dpavlin 47
30     my $serial = $h->{'_PROD_SERIAL_NBR'} || die "no serial?";
31     my $ip_list = $h->{'IP_LIST'} || die "no IP_LIST";
32    
33     if ( my $ip = $serial2ip->{$serial} ) {
34 dpavlin 85 my $ip_only = $ip;
35     $ip_only =~ s/\/\d+//g;
36     if ( $ip eq $ip_list || $ip_only eq $ip_list ) {
37 dpavlin 58 once "OK ",__PACKAGE__," $serial $ip_list";
38 dpavlin 49 return;
39     }
40 dpavlin 47
41     warn "## change_ip $serial ",
42     $h->{'_VARIANT_FRIENDLY_NAME'}, " ",
43     $h->{'_BOARD_NAME'}, " ",
44     $h->{'_BUILD'}, " ",
45 dpavlin 58 "IP $ip_list -> $ip\n" if $debug;
46    
47 dpavlin 47 my $ip_status;
48     $ip_status->{$_}-- foreach ( split(/;/, $ip_list ) );
49 dpavlin 85 $ip_status->{$_}++ foreach ( split(/;/, $ip_only ) );
50 dpavlin 47 foreach my $current_ip ( sort { $ip_status->{$b} <=> $ip_status->{$a} } keys %$ip_status ) {
51     if ( $ip_status->{$current_ip} < 0 ) {
52 dpavlin 58 warn "-- remove IP $current_ip\n" if $debug;
53 dpavlin 47 return "ip ipdelete addr=$current_ip";
54     } elsif ( $ip_status->{$current_ip} > 0 ) {
55 dpavlin 85 warn "++ add IP $current_ip\n" if $debug;
56     return "ip ipadd intf=LocalNetwork addr=$current_ip";
57 dpavlin 47 }
58     }
59     return;
60     } else {
61 dpavlin 85 once "WARNING: unknown serial $serial assigning $ip1;$ip2";
62     $serial2ip->{$serial} = $ip1++ . ';' . $ip2++;
63 dpavlin 47 return;
64     }
65     }
66    
67 dpavlin 51 1;

  ViewVC Help
Powered by ViewVC 1.1.26