Hello Wise and Generous Monks,

I'm trying to write a Perl script that uses Net::SNMP to query RFC 2674 enabled Ethernet switches. One of the "Textual Conventions" defined in the MIB is a "PortList" that the MIB defines as:

PortList ::= TEXTUAL-CONVENTION STATUS current DESCRIPTION "Each octet within this value specifies a set of eight ports, with the first octet specifying ports 1 through 8, the second octet specifying ports 9 through 16, etc. Within each octet, the most significant bit represents the lowest numbered port, and the least significant bit represents the highest numbered port. Thus, each port of the bridge is represented by a single bit within the value of this object. If that bit has a value of '1' then that port is included in the set of ports; the port is not included if its bit has a value of '0'." SYNTAX OCTET STRING

What I would like to do is convert the hex string to an Perl array where the variable is either a one or a zero depending on the hex string. What I've written so far is:

#!/usr/bin/perl -w use strict; use diagnostics; use Data::Dumper; use Net::SNMP; use Carp; my $debug = 0; my $ports = "1.3.6.1.2.1.17.7.1.4.2.1.5.0.1"; my ($session, $error) = Net::SNMP->session( version=> 2, -hostname => "switch", -community=> "public"); if (!$session) { croak("Net::SNMP Session not created\n"); } my $result = $session->get_request(-varbindlist => $ports]); + if (!$result) { my $err = $session->error(); print "ERROR: $err \n"; croak("no results from get_bulk_result"); } $session->close(); my $portstr = $result->{$ports}; print "The port string is: $portstr\n";

Which produces the following output: 0xfffbff0000000000000000

I know I need to use unpack to make the conversion, but I'm not sure how to go about it.

Thanks in advance for your generous help.

Notdeadyet


In reply to Convert SNMP Octet String to Array by Notdeadyet

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.