Okay, I cleaned it up, used strict, warnings and diagnostics which helped me
declare my variables. The code works for 1 switch in the switch file. 

I have only been using PERL seriously for a couple of months and I think my
logic is good because when the switch file has:
X.x.x.12,-c <community string>,1.3.6.1.2.1.17.4.3.1.1,1.3.6.1.2.1.17.4.3.1.2
It works fine, I can separate out into two files, 1 for .12 and 1 for .13
run two separate scripts concurrently and the output file will have it
neatly separated with no mix-ups, but when I have one source file with:
X.x.x.12,-c <community string>,1.3.6.1.2.1.17.4.3.1.1,1.3.6.1.2.1.17.4.3.1.2
X.x.x.13,-c <community string>,1.3.6.1.2.1.17.4.3.1.1,1.3.6.1.2.1.17.4.3.1.2

I get the following type of garbage

X.x.x.13-00 03 93 6D A1 C4 -24
X.x.x.13-00 03 93 6D A1 C4 -24
X.x.x.12-00 03 93 6D A1 C4 -24
X.x.x.13-00 06 5B 76 FC 2D -13
X.x.x.13-00 06 5B 76 FC 2D -13
X.x.x.12-00 06 5B 76 FC 2D -13

Thanks,
Rich 

Added my cleaned up code, and added comments; Any help would be appreciated
**********************************

#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
my $snmpwalk='/usr/local/bin/snmpwalk';
my $ver='-v 1';
my $mastermac='/home/rrichmon/tmp/rich';
my $file='/home/rrichmon/tmp/switches';
my (@stringmac,@stringport,@hostidmac,@hostidport,@hostmacport)=();
my($host,$com,$oid1,$oid2)=0;
#############################################################
#open switches file read each line which is in the format of  IP,Community
strin#g,oid1,oid2
#############################################################
open (FILE, "$file");
foreach my $switchvar(<FILE>) {
        ($host,$com,$oid1,$oid2)=split(/,/, $switchvar);
        walk();
        pre24();
        blade24();
}
close (FILE);

#############################################################
## Use external command due to restriction to SNMP V1 walk
## the two oid's and separate them into two arrays
## oid 1 which is in the following form
## "SNMPv2-SMI::mib-2.17.4.3.1.1.0.0.170.132.228.74 = Hex-STRING: 00 00 AA
84 E4 4A"
##
## oid 2 is in the following form
## "SNMPv2-SMI::mib-2.17.4.3.1.2.0.0.29.232.11.205 = INTEGER: 9"
############################################################
sub walk {
open (SNMPWALK,"$snmpwalk $ver $host $com $oid1|");
while (<SNMPWALK>) {
push @stringmac, "$host-$_";
}
close(SNMPWALK);
open (SNMPWALK,"$snmpwalk $ver $host $com $oid2|");
while (<SNMPWALK>) {
push @stringport, "$host-$_";
}
close(SNMPWALK);
}
################################
## 24 Port pre-processor
## strips out the un-necessary and switch macs, leaves us with
## @hostidmac x.x.x.x-<uniq id>-<uniqmac>
## @hostidport x.x.x.x-<uniq id>-<port#>
################################
sub pre24 {
foreach my $macstring(@stringmac){
        if ($macstring =~
/^(.*)\-SNMPv2-SMI::mib-2.17.4.3.1.1.\d.\d+.(\d+\.\d+\.\d+\.\d+).*\:\s+(.+\s
.+\s.+\s.+\s.+\s.+)/i && ($3 !~ /^00\s00\s1D.*/i)){
push @hostidmac, "$1-$2-$3";
}}
foreach my $portstring(@stringport){
        if ($portstring =~
/^(.*)\-SNMPv2-SMI::mib-2.17.4.3.1.2.\d.\d+.(\d+\.\d+\.\d+\.\d+).*\:\s+(\d+)
/i && ($3 lt 25) && ($3 gt 0)) {
push @hostidport, "$1-$2-$3";
}}
}
###################################################################
## This sub then uses the uniq id to match up the mac and port number
## this also used to output it to a file but I was trying to separate the
## pieces so I could try to troubleshoot
###################################################################
sub blade24 {
foreach my $finalport(@hostidport){
                foreach my $finalmac(@hostidmac){
                my ($host,$id,$m)=split(/-/, $finalmac);
                        if ($finalport =~
/^(\d+\.\d+\.\d+\.\d+)\-(.*)\-(\d+)/i && ($2 eq $id)){
push @hostmacport, "$host-$m-$3";
}}}
}
#######################################################################
## I am simply sending output to a file to be reviewed
#######################################################################
open (MASTER, ">>$mastermac");
foreach my $mactoport(@hostmacport){
print MASTER "$mactoport\n";
}
close(MASTER);


-----Original Message-----
From: Wiggins d Anconia [mailto:[EMAIL PROTECTED] 
Sent: Friday, April 09, 2004 10:31 AM
To: arichmond(contr-ird); '[EMAIL PROTECTED]'
Subject: Re: Help with the logic of my structure?




> 
> I am using snmp to walk a couple of tables on some switches to output
a list
> of mac to port numbers. It works fine when using only 1 switch, when I 
> put multiple switches in the list to walk, the output gets messed up 
> showing multiple ports and or the same port but with different hosts. 
> I have tried to put in "sleep" statements thinking maybe some data was 
> lingering in but that did not help. I also tried replacing the 
> "foreach" with a "while - foreach" on line 7 but then is skips the 
> first switch and only walks the second switch. Any thoughts?  
> Unfortuantely I can't uprgrade my
switches so
> they will accept SNMP v2 so I am stuck with walking instead of a
bulkget. My
> file is structured like this:
>  
> file: switches
> x.x.x.x,-c <community
string>,1.3.6.1.2.1.17.4.3.1.1,1.3.6.1.2.1.17.4.3.1.2
> x.x.x.x,-c <community
string>,1.3.6.1.2.1.17.4.3.1.1,1.3.6.1.2.1.17.4.3.1.2
>  

[snip code]

I will admit I didn't take a long look at your code, I suspect the problem
is as you state lingering values in variables.  

I would suggest scoping your variables properly, turning on strictures with:

use strict;

Naming your variables with something useful rather than $a, $b, S, T, etc.
Dropping ampersands on function calls when they are not needed. Using
explicit variables rather than $_.

Cleaning up these things will make your code readable, will improve your
ability to debug it, etc.  Having completed those tasks if you are still
stumped then check back... maybe some other brave soul will take up the task
without these things....

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to