rspence has asked for the wisdom of the Perl Monks concerning the following question:
EDITOR's NOTE: the following node has been updated from it's original wording. You can view the orginal question here: http://perlmonks.thepen.com/303087_orig.html
Good Evening Reveared monks,
My problem was not in my nested loop - although as clunky (senseless, inefficient you say) I still think it would work - but before I even get to it. As I suspected since I close both while loops that create the 2 hashes *before* I get to the loop perl gives it only the end of each hash to play with. I don't know this for a fact, but, upon leaving the first while open then opening the second fh and second while loop, thus having "everything in memory" the loop works. (I've left the commented lines leaving each while loop open) I have included below the first iteration of the script that works using sauoq's latest loop suggestion, with a slight edit. Now on to the final snmp hash and merging it with this one.
My apologies for misusing any monk's cycles. I asked for a solution to a problem that was misdiagnosed. Alas the socratic method....
#!/usr/bin/perl -w # # # mac-bp-ifIndex.pl - a test script to gather the 1.3.6.1.2.1.17.4.3.1 +.2 OID # and the 1.3.6.1.2.1.17.1.4.1.2 OID information f +rom switches # (via snmp polls) then merge the tuples to create + a single # MAC -> ifIndex pairs list. # # # + use strict; + my $depot_dir="/nms/depot"; my $comm_string="xxx"; my $switch_list="/nms/switch_fetch/switch_list"; my $switch; my $mac; my $bp; my $bp2; my $ifIndex; my $snmpwalk; + my (%mactobp, %bptoifIndex); + open (SWITCH, "$switch_list") || die "Can't open $switch_list file"; open (LOG, ">$depot_dir/mac-bp-ifIndex.txt") || die "Can't open $depot +_dir/mac-bp-ifIndex.txt file"; + while (<SWITCH>) { chomp($switch="$_"); $snmpwalk="/usr/local/bin/snmpwalk -c $comm_string $switch "; + open(MACtoBP, "$snmpwalk .1.3.6.1.2.1.17.4.3.1.2 |") || die "c +an't do .1.3.6.1.2.1.17.4.3.1.2: $!\n"; + while (<MACtoBP>) { /SNMPv2-SMI::mib-2.17.4.3.1.2.(\d+)\.(\d+)\.(\d+)\.(\d ++)\.(\d+)\.(\d+)\s+\=\s+\w+:\s(\d+)$/gm; + $mac=sprintf ("%lx:%lx:%lx:%lx:%lx:%lx",$1, $2, $3, $4 +, $5, $6); $bp=$7; + %mactobp = ( $mac => "$bp" ); + # } + + open(BPtoifIndex, "$snmpwalk .1.3.6.1.2.1.17.1.4.1.2 |") || di +e "can't do .1.3.6.1.2.1.17.1.4.1.2: $!\n"; + while (<BPtoifIndex>) { /SNMPv2-SMI::mib-2.17.1.4.1.2.(\d+)\s+\=\s+\w+:\s(\d+) +$/gm; + $bp2=sprintf ("%d",$1); $ifIndex=sprintf ("%d",$2); + %bptoifIndex = ( $bp2 => "$ifIndex" ); + # } + + for my $mac (keys %mactobp) { if (exists $bptoifIndex{$bp} ) { $mactobp{$mac} = $bptoifIndex{$ifIndex}; print "$mac -> $ifIndex\n"; } } } } + } + close MACtoBP; close BPtoifIndex; close LOG; close SWITCH;
rspence
edited: Mon Nov 3 13:48:02 2003 by jeffa - added editor's note, readmore, formatting
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Merging 2 hashes
by Abigail-II (Bishop) on Oct 29, 2003 at 20:07 UTC | |
by rspence (Initiate) on Oct 30, 2003 at 00:25 UTC | |
|
Re: Merging 2 hashes
by sauoq (Abbot) on Oct 29, 2003 at 20:17 UTC | |
|
Re: Merging 2 hashes
by jonadab (Parson) on Oct 29, 2003 at 20:14 UTC | |
|
Re: Merging 2 hashes
by etcshadow (Priest) on Oct 29, 2003 at 19:51 UTC | |
|
Re: Merging 2 hashes
by Limbic~Region (Chancellor) on Oct 29, 2003 at 20:01 UTC | |
by Abigail-II (Bishop) on Oct 29, 2003 at 20:14 UTC | |
by sauoq (Abbot) on Oct 29, 2003 at 20:29 UTC | |
|
Re: Merging 2 hashes - updated again
by Hena (Friar) on Oct 30, 2003 at 08:19 UTC | |
by rspence (Initiate) on Oct 31, 2003 at 00:46 UTC |