chrisric has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am a novice with Perl which is why I am here :) I have been trying to adapt this Cisco script to collect a bit more info. via SNMP. It normally just collects "Interface, IP-Address, Mask, MTU, Speed, Admin, Operational" and I have added "System Name, Serial Number, System Hardware, Card Description, Card Serial Number".

The problem is if I run it against a router that has 3 IP interfaces and 4 cards/modules I get the 3 IP interfaces and the first 3 of the cards. It misses the last card, I can only guess the interface code is dictating the number of lines to use.

I am sure it is something obvious but it is beyond my current understanding so some help would be greatly appreciated.

Thank you,
Chris

---- Cisco Stats Script ----

#!/usr/bin/perl -w # # $workingdir="/usr/local/scripts/tmp/work"; $snmpro="blah1"; # $rtrlist="$workingdir/RTR_LIST"; $snmpwalk="/usr/local/bin/snmpwalk -v 1 -c $snmpro"; $snmpget="/usr/local/bin/snmpget -v 1 -c $snmpro"; open (RTR, "$rtrlist") || die "Can't open $rtrlist file"; open (CSV, ">$workingdir/RESULT.csv") || die "Can't open RESULT.csv fi +le"; print CSV "Hostname,Interface,IP-Address,Mask,MTU,Speed,Admin,Opera +tional"; while (<RTR>) { chomp($rtr="$_"); @ifIndex=`$snmpwalk $rtr .1.3.6.1.2.1.4.20.1.2`; @ipAddress=`$snmpwalk $rtr .1.3.6.1.2.1.4.20.1.1`; @ipMask=`$snmpwalk $rtr .1.3.6.1.2.1.4.20.1.3`; @cardDescription=`$snmpwalk $rtr enterprises.9.3.6.11.1.3`; @cardSerialN=`$snmpwalk $rtr enterprises.9.3.6.11.1.4`; $arraynum=0; for $ifnumber (@ifIndex) { chomp(($foo, $ifnum) = split(/: /, $ifnumber)); $ifDescription=`$snmpget $rtr ifDescr.$ifnum`; $ifMTU=`$snmpget $rtr ifMtu.$ifnum`; $ifSpeed=`$snmpget $rtr ifSpeed.$ifnum`; $ifAdminstatus=`$snmpget $rtr ifAdminStatus.$ifnum`; $ifOperstatus=`$snmpget $rtr ifOperStatus.$ifnum`; $sysName=`$snmpget $rtr sysName.0`; $sysHware=`$snmpget $rtr sysDescr.0`; $SerialN=`$snmpget $rtr enterprises.9.3.6.3.0`; chomp(($foo, $ipaddr) = split(/: /, $ipAddress[$arraynum])); chomp(($foo, $mask) = split(/: /, $ipMask[$arraynum])); chomp(($foo, $carddes) = split(/: /, $cardDescription[$arraynum] +)); chomp(($foo, $cardsn) = split(/: /, $cardSerialN[$arraynum])); chomp(($foo, $ifdes) = split(/: /, $ifDescription)); chomp(($foo, $mtu) = split (/: /, $ifMTU)); chomp(($foo, $speed) = split (/: /, $ifSpeed)); chomp(($foo, $admin) = split (/: /, $ifAdminstatus)); chomp(($foo, $oper) = split (/: /, $ifOperstatus)); chomp(($foo, $sysname) = split (/: /, $sysName)); chomp(($foo, $syshw) = split (/: /, $sysHware)); chomp(($foo, $sn) = split (/: /, $SerialN)); if ( $speed > 3194967295 ) { $speed = 0 }; $admin =~ s/\(.*\)//; $oper =~ s/\(.*\)//; if ( $oper eq "dormant" ) { $oper = "up(spoofing)"}; $speed = $speed/1000; if ( $speed > 1000) { $speed = $speed/1000; $speed =~ s/$/ Mb\/s/; } else { $speed =~ s/$/ Kb\/s/; } print CSV "\n$sysname,$rtr,$ifdes,$ipaddr,$mask,$mtu,$speed,$adm +in,$oper,$sn,\"$syshw\",$carddes,$cardsn"; $arraynum++; } } close(RTR); close(CSV);

Replies are listed 'Best First'.
Re: Array trouble?
by dragonchild (Archbishop) on May 24, 2004 at 13:26 UTC
    I'm going to guess that @ifIndex has three elements in it. You're using it as your limiting loop.

    A few other comments:

    1. Add use strict; to the top of your script and add scoping to your variables.
    2. Create a function to do the splitting thing.
    3. Reorganize your code so that you do all the things to a given value in one place. For example, you do stuff to $speed in three places. It's difficult to follow what's going on.
    4. Use Text::xSV to write out to your CSV file. The CSV format is actually quite difficult to do correctly - this is what modules are for.
    5. Speaking of modules, I'm willing to bet that there's a module on CPAN to talk to your CISCO routers. Check http://search.cpan.org/.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested