Please bottom post...

> 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. 
>

Sort of... You have declared your variables, all at the top which
defeats what 'strict' will provide. Essentially you are still using
global variables when you shouldn't.  You need to declare the variables
at the point when you use them. Try moving your subs to the top of the
script and see what happens, if they don't break encapsulation then it
will still compile, if they do, it won't. And they do.
 
> I have only been using PERL seriously for a couple of months 

Perl or perl, never PERL.

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
> 

I don't really know why the above is garbage?  Can you provide a sample
of what you expect to show up.

> 
> 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';

It is ok that these are "global" (aka file scoped), though it would be
better if they weren't.  Or possibly make them constants?

> my (@stringmac,@stringport,@hostidmac,@hostidport,@hostmacport)=();
> my($host,$com,$oid1,$oid2)=0;

These should not be declared here.  By doing so they are still not
scoped to the portion of the script they should be.

> #############################################################
> #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();

In general subroutines should take arguments and return values, at least
a result. Then that result should be tested for success/failure, etc. 
These three subs use the "global" nature of the variables you have file
scoped, instead they should receive them as arguments and return values
that are used in the next steps.

> }
> 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|");

Using globals....

> while (<SNMPWALK>) {
> push @stringmac, "$host-$_";

Storing to a global, which means @stringmac never gets cleared.

> }
> 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)){

You only escape the (.) when using it to determine the IP (?) but not in
the other spots, (.) is a special character outside of character
classes, though I suspect your data will be clean enough it shouldn't
matter *most* of the time (what is your definition of production?).


> 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){

Again use of a "global"...

>                 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";

Pushing to a "global"...

> }}}
> }
> #######################################################################
> ## I am simply sending output to a file to be reviewed
> #######################################################################
> open (MASTER, ">>$mastermac");

Always check that open succeeds, and usually you will want to provide
why it doesn't with $!,

open (MASTER, ">>$mastermac") or die "Cannot open file for appending: $!";

I do assume you want to append and not overwrite, if not your ">>"
should be a ">"...

> foreach my $mactoport(@hostmacport){
> print MASTER "$mactoport\n";
> }
> close(MASTER);
> 

[snip old posts]

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