It's not clear from your code what format you're using for $target (the get() functions accepts a bunch of different input formats) - it might be easiest to use an SNMP::Varbind object as the input for get(). An SNMP::Varbind object is a blessed reference to an array with 5 elements:
- object name (e.g. "sysdesc" or ".1.3.6.1.2.1.1.1"
- dotted-decimal instance ID (or "0" for scalar objects)
- value
- value type (e.g. "IPADDR", "COUNTER", etc.)
- timestamp

You can set the name of the object, and get() will populate the rest of the info, which you can then test to figure out what format the return value is in. Assuming your input target is a string like "sysDescr" or ".1.3.6.1.2.1.1.1", you should be able to do something like:

my $var = new SNMP::Varbind( [ $target ] ); my $value = $session->get($var) || warn "SNMP ERROR: $session->{ErrorStr}"; print "name: ", $var->name, "\n"; print "type: ", $var->type, "\n"; if ( $var->type eq "IPADDR" ) { ... unpack $var->val here ... } elsif ( $var->type eq "COUNTER" ) { ... unpack $var->val here ... } elsif ...

The SNMP man page will list all the valid variable types that you might need to deal with.


In reply to Re: To unpack or not to unpack by larryl
in thread To unpack or not to unpack by ergophobe

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.