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

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

Replies are listed 'Best First'.
Re: Convert SNMP Octet String to Array
by Illuminatus (Curate) on Sep 30, 2008 at 21:28 UTC
    From http://perldoc.perl.org/functions/vec.html
    @bits = split(//, unpack("b*", $vector));
Re: Convert SNMP Octet String to Array
by GrandFather (Saint) on Sep 30, 2008 at 22:00 UTC

    If I understand what you are asking then:

    use warnings; use strict; my $ports = '0xFFFBFF0000000000000000'; my $basePort = 1; my @ports; $ports = substr $ports, 2; while ($ports) { my $octet = hex substr $ports, 0, 2, ''; my $index = 0; while ($octet) { next unless $octet & 0x80; push @ports, $basePort + $index; } continue { ++$index; $octet = ($octet << 1) & 0xff; } $basePort += 8; } print join '.', @ports;

    which prints:

    1.2.3.4.5.6.7.8.9.10.11.12.13.15.16.17.18.19.20.21.22.23.24

    seems to be a solution, although I don't see how that relates to the initial port string in your sample code.


    Perl reduces RSI - it saves typing
Re: Convert SNMP Octet String to Array
by moritz (Cardinal) on Sep 30, 2008 at 21:27 UTC
    Maybe vec or unpack can help you (see also perlpacktut for a gentler introduction, but vec() is probably more appropriate).