An indication of which Virtual LANs are allowed on this Inter-Switch Link. This is an octet string value with bits set to indicate allowed VLANs. It can be interpreted as a sum of f(x) as x goes from 0 to 1023, where f(x) = 0 for VLAN x not allowed and f(x) = exp(2, x) for VLAN x allowed.

Some genius wrote that documentation, presumably thinking it was wonderfully precise, without specifying the order of bits in the 'octet string'

If we make the flying assumption that the '0xfff...fe' is a 1024 bit number, and not an 'octet string' at all, then we can go to and from a Perl bit-vector string, and use vec to access it, as in the code below.

The result is:

  Ranges: 1..1023
which looks plausible. But a value with a few more '0' bits in it would be a stronger test.

use strict ; use warnings ; #1234567890123456789012345678901234567890123456789012345 +678901234 my $vlans = '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffff +fffffffff' .'fffffffffffffffffffffffffffffffffffffffffffffffffffffff +fffffffff' .'fffffffffffffffffffffffffffffffffffffffffffffffffffffff +fffffffff' .'fffffffffffffffffffffffffffffffffffffffffffffffffffffff +ffffffffe' ; my $vlan_vec = to_vec($vlans) ; # Convert long hex integer to bit-vect +or # VLAN numbers are mapped to/from $vlan_vec by vec($vlan_vec, 1, $vn) # This scans the bit-vector looking for ranges of VLAN numbers. # # Note that we can read one bit beyond the given range, and get 0 my $r = undef ; my @s = () ; for my $vn (0..length($vlan_vec) * 8) { if (vec($vlan_vec, $vn, 1)) { if (!defined($r)) { push @s, "$vn.." ; } ; $r = $vn ; } else { if (defined($r)) { $s[-1] .= "$r" ; $r = undef ; } ; } ; } ; print "Ranges: ", join(', ', @s), "\n" ; # Convert long hex integer to bit-vector. # # Combination of 'h*' and reverse gives bits in right order in the bit +-vector. sub to_vec { my ($hex) = @_ ; $hex =~ s/^0x// ; return pack('h*', scalar reverse $hex) ; } ; # Convert bit-vector to long integer. Again 'h*' and reverse sorts ou +t bit order. sub from_vec { my ($vec) = @_ ; return '0x'. scalar reverse(unpack('h*', $vec)) ; } ;

In reply to Re: Allowed VLANs with SNMP by gone2015
in thread Allowed VLANs with SNMP by spivey49

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.