Subversion Repositories sysadmin_scripts

Rev

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

Rev 130 Rev 131
Line 169... Line 169...
169
            next if $connection eq 'alias' or $connection eq 'description';
169
            next if $connection eq 'alias' or $connection eq 'description';
170
            &initialize( $thisPort,$connection,'mac','ip','hostname','lastseen' );
170
            &initialize( $thisPort,$connection,'mac','ip','hostname','lastseen' );
171
            if ( $switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'mac'} eq $mac ) {
171
            if ( $switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'mac'} eq $mac ) {
172
               my $modified = &updateValue( \$switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'ip'}, $ip );
172
               my $modified = &updateValue( \$switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'ip'}, $ip );
173
               $modified |= &updateValue( \$switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'hostname'},gethostbyaddr( inet_aton( $ip ), AF_INET ));
173
               $modified |= &updateValue( \$switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'hostname'},gethostbyaddr( inet_aton( $ip ), AF_INET ));
174
               $switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'lastseen'} = time() if $modified;
174
               $switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'lastseen'} = time();
175
               return;
175
               return;
176
            } # if we found it
176
            } # if we found it
177
         } # for connection
177
         } # for connection
178
      } # for port
178
      } # for port
179
   } # for switch
179
   } # for switch
Line 237... Line 237...
237
   use File::Basename;
237
   use File::Basename;
238
   print basename($0) . " $VERSION\n";
238
   print basename($0) . " $VERSION\n";
239
   print <<END
239
   print <<END
240
$0 [options]
240
$0 [options]
241
Options:
241
Options:
-
 
242
   --no-snmp        - do not actually run snmp commands
242
   --debug          - set debug level
243
   --debug          - set debug level
243
   --version        - display version and exit
244
   --version        - display version and exit
244
   --help           - This page
245
   --help           - This page
245
END
246
END
246
}
247
}
247
 
248
 
248
 
249
 
249
# handle any command line parameters that may have been passed in
250
# handle any command line parameters that may have been passed in
250
my $version = 0; # just used to determine if we should display the version
251
my $version = 0; # just used to determine if we should display the version
251
my $help = 0; # also if we want help
252
my $help = 0; # also if we want help
-
 
253
my $nosnmp = 0; # we do NOT want to run snmp commands
252
GetOptions (
254
GetOptions (
253
            'debug|d=i'     => \$DEBUG,
255
            'debug|d=i'     => \$DEBUG,
-
 
256
            'nosnmp|n'      => \$nosnmp,
254
            'help|h'        => \$help,
257
            'help|h'        => \$help,
255
            'version|v'     => \$version,
258
            'version|v'     => \$version,
256
            ) or die "Error parsing command line\n";
259
            ) or die "Error parsing command line\n";
257
 
260
 
258
                  
261
                  
Line 273... Line 276...
273
   %switchports = %{ $yaml->[0] };
276
   %switchports = %{ $yaml->[0] };
274
}
277
}
275
 
278
 
276
# first, get all of the MAC/Port assignments from the switches
279
# first, get all of the MAC/Port assignments from the switches
277
foreach my $switch ( keys %{$config{'switches'}} ) {
280
foreach my $switch ( keys %{$config{'switches'}} ) {
278
   #last;
281
   last if $nosnmp; # do not read switches if $nosnmp set
279
   print "Working on $switch\n" if $DEBUG;
282
   print "Working on $switch\n" if $DEBUG;
280
   &initialize( \%switchports, $switch, 'name','location' );
283
   &initialize( \%switchports, $switch, 'name','location' );
281
   &updateValue(
284
   &updateValue(
282
      \$switchports{$switch}{'name'},
285
      \$switchports{$switch}{'name'},
283
      &getOneSNMPValue( $DEVICENAMEMIB,$config{'switches'}{$switch}{'community'},$switch, qr/= STRING: "?([^"]*)"?/ )
286
      &getOneSNMPValue( $DEVICENAMEMIB,$config{'switches'}{$switch}{'community'},$switch, qr/= STRING: "?([^"]*)"?/ )
Line 340... Line 343...
340
#print Dumper( \%switchports ); die;
343
#print Dumper( \%switchports ); die;
341
 
344
 
342
 
345
 
343
# Now, try to match up the MAC address. Read the ARP table from the router(s)
346
# Now, try to match up the MAC address. Read the ARP table from the router(s)
344
foreach my $router ( keys %{$config{'routers'}} ) {
347
foreach my $router ( keys %{$config{'routers'}} ) {
345
   # last;
348
   last if $nosnmp; # do not read routers if $nosnmp set
346
   print "Working on $router\n" if $DEBUG;
349
   print "Working on $router\n" if $DEBUG;
347
   print "snmpwalk -v1 -c $config{routers}{$router}{community} $router $ROUTERIPMACMIB\n" if $DEBUG > 1;
350
   print "snmpwalk -v1 -c $config{routers}{$router}{community} $router $ROUTERIPMACMIB\n" if $DEBUG > 1;
348
   my $values = `snmpwalk -v1 -c $config{routers}{$router}{community} $router $ROUTERIPMACMIB`;
351
   my $values = `snmpwalk -v1 -c $config{routers}{$router}{community} $router $ROUTERIPMACMIB`;
349
   my @lines = split( "\n", $values );
352
   my @lines = split( "\n", $values );
350
   foreach my $line ( @lines ) {
353
   foreach my $line ( @lines ) {
351
      $line =~ m/$ROUTERIPMACMIB\.([0-9]+)\.([0-9.]+)\s=\sHex-STRING:\s([0-9a-z ]+)/i;
354
      $line =~ m/$ROUTERIPMACMIB\.([0-9]+)\.([0-9.]+)\s=\sHex-STRING:\s([0-9a-z ]+)/i;
352
      my $interface = $1;
355
      my $interface = $1;
353
      my $ip = $2;
356
      my $ip = $2;
354
      my $mac = cleanMac( $3 );
357
      my $mac = &cleanMac( $3 );
355
      #$mac =~ s/ //g;
358
      #$mac =~ s/ //g;
356
      &updateIP( \%switchports, $ip, $mac );
359
      &updateIP( \%switchports, $ip, $mac );
357
   }
360
   }
358
}
361
}
359
 
362
 
Line 383... Line 386...
383
   foreach my $portnum (keys %{ $switchports{$switch}{'ports'} } ) {
386
   foreach my $portnum (keys %{ $switchports{$switch}{'ports'} } ) {
384
      foreach my $connection ( keys %{ $switchports{$switch}{'ports'}{$portnum} } ) {
387
      foreach my $connection ( keys %{ $switchports{$switch}{'ports'}{$portnum} } ) {
385
         next if $connection eq 'description' or $connection eq 'alias';
388
         next if $connection eq 'description' or $connection eq 'alias';
386
         my $mac = $switchports{$switch}{'ports'}{$portnum}{$connection}{'mac'};
389
         my $mac = $switchports{$switch}{'ports'}{$portnum}{$connection}{'mac'};
387
         # we already have a hostname and IP, so skip to next one
390
         # we already have a hostname and IP, so skip to next one
388
         #next if $switchports{$switch}{'ports'}{$portnum}{$connection}{'ip'} && $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'};
-
 
389
         # print "Looking up $switch, port $portnum with mac address $mac\n";
391
         # print "Looking up $switch, port $portnum with mac address $mac\n";
390
         if ( defined ( $config{'staticmaps'}{$mac} ) ) {
392
         unless ( $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'} ) {
391
            if ( $config{'staticmaps'}{$mac}{'ip'} && ( $config{'staticmaps'}{$mac}{'override'} || ! $switchports{$switch}{'ports'}{$portnum}{$connection}{'ip'} ) ) {
393
            if ( defined( $config{'staticmaps'}{$mac}{'ip'} ) && $config{'staticmaps'}{$mac}{'hostname'} ) {
392
               $switchports{$switch}{'ports'}{$portnum}{$connection}{'ip'} = $config{'staticmaps'}{$mac}{'ip'};
394
               $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'} =  $config{'staticmaps'}{$mac}{'hostname'};
-
 
395
            } elsif ( defined( $nonRouterEntries->{$mac}->{'hostname'} ) && $nonRouterEntries->{$mac}->{'hostname'} ) {
-
 
396
               $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'} =  $nonRouterEntries->{$mac}->{'hostname'};
393
            }
397
            }
-
 
398
         }
-
 
399
         unless ( $switchports{$switch}{'ports'}{$portnum}{$connection}{'ip'} ) {
394
            if ( $config{'staticmaps'}{$mac}{'hostname'} && ( $config{'staticmaps'}{$mac}{'override'} || ! $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'} ) ) {
400
            if ( defined( $config{'staticmaps'}{$mac}{'ip'} ) && $config{'staticmaps'}{$mac}{'ip'} ) {
395
               $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'} = $config{'staticmaps'}{$mac}{'hostname'};
401
               $switchports{$switch}{'ports'}{$portnum}{$connection}{'ip'} =  $config{'staticmaps'}{$mac}{'ip'};
-
 
402
            } elsif ( defined( $nonRouterEntries->{$mac}->{'ip'} ) && $nonRouterEntries->{$mac}->{'ip'} ) {
-
 
403
               $switchports{$switch}{'ports'}{$portnum}{$connection}{'ip'} =  $nonRouterEntries->{$mac}->{'ip'};
396
            }
404
            }
397
         } # if there is an entry
405
         }
398
         $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'} = $nonRouterEntries->{$mac}->{'hostname'}
-
 
399
            if ( ! $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'} && $nonRouterEntries->{$mac}->{'hostname'} );
-
 
400
         $switchports{$switch}{'ports'}{$portnum}{$connection}{'ip'} = $nonRouterEntries->{$mac}->{'ip'}
-
 
401
            if ( ! $switchports{$switch}{'ports'}{$portnum}{$connection}{'hostname'} && $nonRouterEntries->{$mac}->{'ip'} );
-
 
402
      } # checking each connection
406
      } # checking each connection
403
   } # checking each port number
407
   } # checking each port number
404
} # checking each switch
408
} # checking each switch
405
 
409
 
406
#die Dumper( \%switchports );
410
#die Dumper( \%switchports );