#!/opt/vdops/bin/perl # This script lists the CDP neighbors of the target # V Who When What # --------------------------------------------------------------------------- # 1.2.0 skendric 2011-02-21 Upgrade to Netops 1.4.0 # 1.1.0 skendric 2010-02-05 Upgrade to perl 5.10.1 # 1.0.0 skendric 2007-08-17 First Version # # Author: Stuart Kendrick, sbk {put at sign here} skendric {put dot here} com # # Source: http://www.skendric.com/device # # This software is available under the GNU GENERAL PUBLIC LICENSE, see # http://www.fsf.org/licenses/gpl.html # # # This script takes the following approach: # -Accepts a command-line list of targets (if empty, employs a # hard-coded list) # -Queries the targets for cdpCacheDeviceId # -Displays the results # # # Requirements: # -The following MIB modules stashed in /opt/vdops/share/snmp/mibs, # or wherever it is that you store MIB modules: # CISCO-PRODUCTS-MIB.my # # -PERL modules: the WI::Netops collection # # # Assumptions: # # # Tested on: # -perl-5.12.2 # -net-snmp-5.6 # # # Instructions: # -Customize the script for your site: find the 'user-configurable # variables' section and modify as appropriate # -Type "show-cdp-neighbors" to see the command-line options # -Try it out # # # # Caveats: # # # Known Bugs: # # # To do: # -Add support for SNMPv3 # # Begin script # Load modules use strict; use warnings; use feature 'say'; use feature 'switch'; use Carp qw(carp cluck croak confess); use Data::Dumper; use English qw( -no_match_vars ); use List::MoreUtils qw(uniq); use Getopt::Std; use WI::Netops::HostTools 1.0.4; use WI::Netops::NetopsTools 2.2.3; use WI::Netops::NetopsData 1.4.0; use WI::Netops::PingTools 1.1.7; use WI::Netops::SNMPTools 1.5.3; use WI::Netops::Utilities 1.4.4; # Declare global variables my %neighbor; # Hash of references to arrays, keyed by target my $telephones; # Boolean expressing our willingness to list # telephones # Define global variables $program_name = 'show-cdp-neighbors'; $usage = 'Usage: show-cdp-neighbors -s {yes|no} [-d {integer}] [-t] [-r] [-a | -e {expr} | -f {filename} | target1 target2 target3 ...]'; $version = '1.2.0'; # Grab arguments getopts('ad:e:f:rs:t', \%option); $telephones = defined $option{t} ? 1 : 0; @target = @ARGV; # Set mode if ($option{r}) { $mode = 'report' } elsif (-t STDIN) { $mode = 'interactive' } else { $mode = 'batch' } ### Begin Main Program ############################################### { check_args(); # Check arguments read_config(); # Read Netops config file compile_mibs(); # Compile MIB files build_target(); # Populate @target target_check(); # Look for errors in @target basic_info(); # Gather information do_the_work(); # Do the work print_report(); # Print report } ##### End Main Program ############################################### ######################################################################## # Do the work: acquire image name ######################################################################## sub do_the_work { my $val; # Debug trace trace_location('begin') if $debug; # Notify operator print_it('Acquiring neighbor information...'); # Loop through targets TARGET: for my $target (@target) { # Walk cdpCacheDeviceId say 'Walking cdpCacheDeviceId' if $debug > 3; my @neighbors; $val = snmpWalk( {host => $target, oid => 'cdpCacheDeviceId'} ); for my $varbind (@$val) { my $neighbor = $varbind->{val}; # Strip serial number ($neighbor) = ($neighbor =~ /\((.*)\)/) if $neighbor =~ /\(/; # Strip domain name ($neighbor) = ($neighbor =~ /(.*?)\./) if $neighbor =~/\./; push @neighbors, $neighbor if defined $neighbor; } if (@neighbors > 0) { @neighbors = uniq @neighbors; @neighbors = sort @neighbors; } $neighbor{$target} = \@neighbors; # Entertain operator print $BANG if $mode eq 'interactive'; } # Make things look pretty say "\n" if $mode eq 'interactive'; # Debug info if ($debug > 2) { say 'Dumping %neighbor'; say Dumper(%neighbor) . "\n"; } # Debug trace trace_location('end') if $debug; return 1; } ######################################################################## # Tell the operator what I discovered ######################################################################## sub print_report { my @fields; my $handle; my $total = @target; my $now = get_now(); # Debug trace trace_location('begin') if $debug; # If we are running in test mode, skip this routine unless ($dome) { print_it("Running in test mode, cannot print a meaningful report\n"); return 1; } # Direct output to screen or to file if ($mode eq 'interactive') { $handle = *STDOUT; } else { open $handle, '>', $report_file or die "Cannot open $report_file: $!\n"; } if ($mode eq 'report') { print {$handle} < 62) { # If this is the first line, just print it. Otherwise, prepend spaces if ($first) { print {$handle} "$string\n"; $first = 0; $string = "$neighbor "; } else { print {$handle} " $string\n"; $string = $EMPTY_STR; $string = "$neighbor "; } } else { $string .= "$neighbor "; } } # Print whatever remains if (length($string) > 0) { if ($first) { print {$handle} "$string\n"; } else { print {$handle} " $string\n"; } } } unless ($handle =~ /STDOUT/) { close $handle or warn "Cannot close $report_file: $!\n"; } # Make things look pretty log_it("Ending $PROGRAM_NAME"); print_it("\n\nEnding $PROGRAM_NAME"); # Debug trace trace_location('end') if $debug; return 1; } ######################################################################## # Output help ######################################################################## sub HELP_MESSAGE { print <