Subversion Repositories sysadmin_scripts

Rev

Rev 69 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 69 Rev 128
Line 187... Line 187...
187
         $return->{$2} = $1;
187
         $return->{$2} = $1;
188
      }
188
      }
189
   }
189
   }
190
   return $return;
190
   return $return;
191
}
191
}
-
 
192
 
-
 
193
# load tab delimited values from $filename into hashref $nonRouterEntries
-
 
194
# format is
-
 
195
# macaddress
-
 
196
#     'ip' = IP Address
-
 
197
#     'mac' = MAC Address
-
 
198
#     'hostname' = HostName
-
 
199
 
-
 
200
sub loadNonRouterEntries {
-
 
201
   my ( $nonRouterEntries, $filename ) = @_;
-
 
202
   open FN,"<$filename" or die "Could not open $filename: $!\n";
-
 
203
   my $line = <FN>;
-
 
204
   chomp $line;
-
 
205
   my @headers = split( "\t", $line );
-
 
206
   my $macIndex = 0;
-
 
207
   while ( $headers[$macIndex] !~ 'mac' && $macIndex < @headers ) {
-
 
208
      $macIndex++;
-
 
209
   }
-
 
210
   if ( $macIndex >= @headers ) {
-
 
211
      warn "File $filename does not appear to contain a mac column\n";
-
 
212
      return;
-
 
213
   }
-
 
214
   while ( $line = <FN> ) {
-
 
215
      next if $line =~ m/^#/;
-
 
216
      chomp $line;
-
 
217
      my @values = split( "\t", $line );
-
 
218
      for( my $i = 0; $i < @headers; $i++ ) {
-
 
219
         $nonRouterEntries->{lc $values[$macIndex]}->{$headers[$i]} = lc $values[$i];
-
 
220
      }
-
 
221
   }
-
 
222
   close FN;
-
 
223
}
-
 
224
   
192
   
225
   
193
# simple display if --help is passed
226
# simple display if --help is passed
194
sub help {
227
sub help {
195
   use File::Basename;
228
   use File::Basename;
196
   print basename($0) . " $VERSION\n";
229
   print basename($0) . " $VERSION\n";
Line 311... Line 344...
311
      $mac =~ s/ //g;
344
      $mac =~ s/ //g;
312
      &updateIP( \%switchports, $ip, $mac );
345
      &updateIP( \%switchports, $ip, $mac );
313
   }
346
   }
314
}
347
}
315
 
348
 
-
 
349
# load non-router entries, if they exist
-
 
350
my $nonRouterEntries = {};;
-
 
351
if ( defined ( $config{'nonrouter'} ) ) {
-
 
352
   foreach my $filename ( @{$config{'nonrouter'}} ) {
-
 
353
      &loadNonRouterEntries( $nonRouterEntries, $scriptDir . '/' . $filename );
-
 
354
   }
-
 
355
}
-
 
356
 
-
 
357
# die Dumper( $nonRouterEntries ) . "\n";
-
 
358
 
316
# now, process any static maps we might have
359
# now, process any static maps we might have and, failing that, any non-router entries
317
if ( defined( $config{'staticmaps'} ) ) {
360
if ( defined( $config{'staticmaps'} ) ) {
318
   # let's make sure there is an entry for each one
361
   # let's make sure there is an entry for each one
319
   foreach my $entry ( keys %{$config{'staticmaps'}} ) {
362
   foreach my $entry ( keys %{$config{'staticmaps'}} ) {
320
      &initialize( \%{$config{'staticmaps'}}, $entry, 'ip','hostname','override' );
363
      &initialize( \%{$config{'staticmaps'}}, $entry, 'ip','hostname','override' );
321
   }
364
   }
Line 332... Line 375...
332
                  $switchports{$switch}{'ports'}{$portnum}{$connection}{'ip'} = $config{'staticmaps'}{$mac}{'ip'};
375
                  $switchports{$switch}{'ports'}{$portnum}{$connection}{'ip'} = $config{'staticmaps'}{$mac}{'ip'};
333
               }
376
               }
334
               if ( $config{'staticmaps'}{$mac}{'hostname'} && ( $config{'staticmaps'}{$mac}{'override'} || ! $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'} ) ) {
377
               if ( $config{'staticmaps'}{$mac}{'hostname'} && ( $config{'staticmaps'}{$mac}{'override'} || ! $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'} ) ) {
335
                  $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'} = $config{'staticmaps'}{$mac}{'hostname'};
378
                  $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'} = $config{'staticmaps'}{$mac}{'hostname'};
336
               }
379
               }
-
 
380
               $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'} = $nonRouterEntries->{$mac}->{'hostname'}
-
 
381
                  if ( ! $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'} && $nonRouterEntries->{$mac}->{'hostname'} );
-
 
382
               $switchports{$switch}{'ports'}{$portnum}{$connection}{'ip'} = $nonRouterEntries->{$mac}->{'ip'}
-
 
383
                  if ( ! $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'} && $nonRouterEntries->{$mac}->{'ip'} );
337
            } # if there is an entry
384
            } # if there is an entry
338
         } # checking each connection
385
         } # checking each connection
339
      } # checking each port number
386
      } # checking each port number
340
   } # checking each switch
387
   } # checking each switch
341
} # if defined
388
} # if defined
-
 
389
 
-
 
390
# finally, look for any mac's that do not have an IP or hostname, and get them from non-router files, if that exists
-
 
391
 
342
            
392
 
343
#die Dumper( \%switchports );
393
#die Dumper( \%switchports );
344
 
394
 
345
# save the state file for later, so we can find things which disappear from the network
395
# save the state file for later, so we can find things which disappear from the network
346
my $yaml = YAML::Tiny->new( \%switchports );
396
my $yaml = YAML::Tiny->new( \%switchports );
347
$yaml->write( $STATEFILE );
397
$yaml->write( $STATEFILE );