in reply to Cisco SNMP CDP Poll
Since you asked, here's my thoughts on Cisco SNMP CDP Poll. Keep in mind that IANAJAPH, so Wiser Monks Than ITM may tell you I'm fullabeans {grin}
First off - I like it, and will tweak it for use instead of some of my own Net::Telnet::Cisco-based scripts for query-only operations. Net::Snmp++
use strict; is your friend, despite sounding rather stern 8^)
pod is also your friend. To my eye, that many embedded comments reduce readability
Use subroutines only for modular functionality (cdp-neighbor, port count, model, etc.) A wise monk recently advised me to think of subroutines as mini-programs instead of chapters in a book. Reduces unecessary global variables, and offers potential for improved readability.
Assign variable names to $_[0] and $_[1], etc early in subroutine. Improved readability?? references?
Combine into chomp($community = <STDIN>);
die "usage: $0 seedip" unless ( $seedhost =~ m{\d+\.\d+\.\d+\.\d+} ) Improved readability??
die "usage: $0 seedip" unless ( @ARGV == 1 ) Improved readability??
Use a CPAN module to parse IP address for legality. m/\d{1,3}.\d{1,3}.\d{1,3},\d{1,3}/ would be small improvement, checking for 1-to3-digit numbers.
Mixed-case subroutine names for clarity. There are a few links to relevant posts on my homenode, near the middle in the "educate" section.
++fingers for verbish subroutine names.
cheers,
Don
striving toward Perl Adept
(it's pronounced "why-bick")
Update 2001-05-17 19:45
The following adds a configurable delay:
my $delay = '2'; near head of script along with other variable declarations, and
sleep($delay); in get_oids() just after write;
Save results to an output file:
my $outfile = 'cdppoll.out'; near head of script along with other variable declarations
open (OUTFILE, ">$outfile") or die"Error opening $outfile WO:$!"; between "chomp" and "@todo"
format OUTFILE = (same as format STDOUT) right after "format STDOUT"
close OUTFILE or die"Error closing $outfile:$!"; right after above "format OUTFILE"
Use x operator to simplify code line for visual output divider, and reduce code width.
print "\n", '=' x 94, "\n";
I don't have these coded yet, but could be good to:
- csv outfile for easy manipulation and importing
- generate summarized outfile listing only unique devices
- check hostname (in addition to IP address) for 'already done'. Avoid discovering same router twice if redundant paths.
- use Term::ReadKey for no-echo entry of SNMP community string.
- use hash(es) to reduce number of global variables: my %oid; $oid{root}, $oid{starting}, $oid{new} instead of $oid_root, $oid_starting, $oid_new
Update 2001-05-17 20:45
"multiple devices w/same hostname..." Ah, I hadn't thought of that. How about an SNMP query for *all* interface addresses then add device to list only if no match? I've not given any thought to the logic, just searching for an easy identifier of "unique".
|
---|
Replies are listed 'Best First'. | |
---|---|
Re:Re: Cisco SNMP CDP Poll (Hostnames may not be unique)
by fingers (Acolyte) on May 18, 2001 at 05:29 UTC |