What I'm trying to do:

I have to get the SNMP value from several Object Identifiers (OIDs) that mean the same thing, but aren't present on all devices. IE: A Cisco 6509 may support the .chassisId OID, but the next device doesn't. I don't want to have to write 18 different subs for all the different devices I have.

What I want to happen is have the getSnmpInfo sub recognize that it was passed a list variable then run itself against each list entry. And if it was passed a scalar, just run against that one value.

I suppose one kludge is to collapse the list with a delimiter, search for it in the sub and expand the list if found.

I seems like there must be a better way though. It feels like I'm missing something fundamental here. And it doesn't help that my previous XP is all in PHP, bash, and .bat files *shudder*

Please excuse my coding if you find it hideous... I'm a newbie to Perl.

Here's the sub:

# main section my ( $sysChassisID, #and other stuff ); # define OID of system Chassis ID attribute (serial number) my @sysChassisIDOID = ( "1.3.6.1.4.1.9.3.6.3", # .chassisId "1.3.6.1.2.1.47.1.1.1.1.11.0", # .entPhysicalSerialNumber.0 "1.3.6.1.2.1.47.1.1.1.1.11.1", # .entPhysicalSerialNumber.1 "1.3.6.1.4.1.9.5.1.2.19.0", # .CiscoStackMIB.chassisSerialNumbe +rString ); # calling the sub: (this takes place inside a while loop as # the script goes through a list of IP addresses. # I borrowed heavily from cdppoll.pl. $sysChassisID = getSnmpInfo (@sysChassisIDOID) sub getSnmpInfo { my($crap , $value , $result); my $varType = ref($_[0]); # Get the variable type from ref() if ($varType eq "ARRAY") { my @tmp = $_[0]; print "\n DEBUG::GET_SNMP_INFO: OID is an array" if $debugg +ing; # march through the array until a valid value is collected the +n # return that value. while (@tmp) { my $tOID = shift @tmp; $value = getSnmpInfo($tOID); return $value if ((ref($value) eq "SCALAR") and ($value =~ m/[ +A-Za-z0-9]{1,}/)); next; } } elsif ($varType eq "SCALAR") { print "\n DEBUG::GET_SNMP_INFO: OID is a scalar" if $de +bugging; } else { print "\n DEBUG:GET_SNMP_INFO:: OID is an illegal varia +ble type ($varType)" if $debugging; return 0; } my($oid) = $_[0]; $session = (defined $_[1]) ? ($_[1]) : ($session); unless ($session) { print "\n DEBUG::GET_SNMP_INFO: session not established in +getSnmpInfo sub-routine, check scopes and pass as option 2 to getSnmp +Info" if $debugging; print "\n WARN::GET_SNMP_INFO: getSnmpInfo returning null, +session unavailable."; return 0; } print "\n DEBUG::GET_SNMP_INFO: fetching OID: $oid" if $debuggi +ng; $result = $session->get_request("$oid"); return unless (defined $result); ($crap , $value) = %$result; print "\n DEBUG::GET_SNMP_INFO: OID value: $value" if $debuggin +g; return $value; }
Thanks for taking your time. - Rowshi

In reply to data type discovery inside sub: how? by rowshi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.